2

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].

https://jsfiddle.net/DTcHh/39987/

1
  • 2
    Your logic is rather odd. Why bother making an array at all when you can just use $('#task3') to get the element you require...? If you want that element's index, use index(). 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-6 elements anyway, not the child #taskN elements. It may be better if you stated what you wanted to achieve, as this does not seem optimal. Commented Dec 7, 2017 at 10:06

3 Answers 3

2

If you want to get index of element inside .col-md-6:last you can use index() function

$(document).ready(function() {

  var index = $(".col-md-6:last #task3").index();
  

  console.log(index);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

Sign up to request clarification or add additional context in comments.

1 Comment

okey this is very simple with the index function. This is working for me, thank you. Dziekuje
2

The array is a list of HTML node elements, so you must pass the node element you want to search, not the string:

$(document).ready(function() {

	$childArr = $(".col-md-6:last").children().toArray();
  
  $indexNumber = $.inArray($("div#task3")[0], $childArr);
  
  console.log($indexNumber);
  console.log($childArr);
});
/* Latest compiled and minified CSS included as External Resource*/

/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');

body {
    margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

2 Comments

By the way, what you want to achieve is pretty weird. If you tell us why you want to do this we can try to help you further.
This was just a sample code to understand how can i get a position in a div. My task are dynamic. They change in time. I have 2 columns (.col-md-6). In these two columns i have 8 columns (.col-md-3). And in this 8 columns i have some bootstrap panels. If there are more than 10 panels so switch the rest of this panel to the other column. For this purpose i need sometime the position of the elements. is hard to describe it
0

As Sylwek and Suren Srapyan answers you don't have to use toArray() to accomplish what you want. However if you want to keep this logic you can use the findIndex() function

$(document).ready(function() {

  $childArr = $(".col-md-6:last").children().toArray();
  
  $indexNumber = $childArr.findIndex(function(child){return child.id == "task3"})
  
  console.log($indexNumber);
  console.log($childArr);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.