The for...in statement iterates over the enumerable properties of an object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
In this case jQuery element (the output of $
function) is not enumerable properties
but a jQuery object. So the right way is to use $().each
Iterate over a jQuery object, executing a function for each matched element.
Suppose your html is partial similar to the code below:
function createInputsForWebAppRemotes() {
var titles = $('.collapseTitle');
titles.each(function() {
var title = $(this).html();
console.log(title);
if (title.indexOf("(CSR)") > -1 || title.indexOf("(ADMIN)") > -1) {
$('#remoteWebAppDiv').append('<input class="form-control" name="remoteWebAppUrl" type="url"/>');
}
});
}
createInputsForWebAppRemotes();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="collapseTitle">Title 1</div>
<div class="collapseTitle">(CSR)</div>
<div class="collapseTitle">Title 3</div>
<div class="collapseTitle">(ADMIN)</div>
<div id="remoteWebAppDiv"></div>
Also as @Mihai T mention, you have problems with the usage of indexOf
:
- You need to check of the index is greater than -1 because this is the output of this function for string which no included in another string.
- The
||
need to be between sentences but not as argument in indexOf
function.