I would like to search for a specific value in an array and return its index.
html:
<div class="container">
<div class="col-md-6">
<div id="task0">Task0</div>
</div>
<div class="col-md-6">
<div id="task1">Task1</div>
<div id="task2">Task2</div>
<div id="task3">Task3</div>
<div id="task4">Task4</div>
</div>
</div>
jquery:
$(document).ready(function() {
$childArr = $(".col-md-6:last").children().toArray();
$indexNumber = $.inArray("div#task3", $childArr);
console.log($indexNumber);
});
Unfortunately I get -1. This means that the specified value was not found. But why do I get the -1 ?
If i output the $childArr in console.log I get an array with this values (4) [div#task1, div#task2, div#task3, div#task4].
$('#task3')to get the element you require...? If you want that element's index, useindex(). As for your code, it doesn't work because you're looking for the index of a string in an array of jQuery objects - and those jQuery objects hold the.col-md-6elements anyway, not the child#taskNelements. It may be better if you stated what you wanted to achieve, as this does not seem optimal.