user1719210 user1719210 - 5 months ago 27
jQuery Question

Jquery : Get the index of non-siblings elements of the closest class

I'm trying to get the index of a non sibling input[type='text'] element within a particular class. Here is the code :

<form class="myForms">
<input class="myClass" type="text" /> -> expected value 0
<input class="myClass" type="text" /> -> expected value 1
<input class="myClass" type="text" /> -> expected value 2
</form>

<form class="myForms">
<input class="myClass" type="text" /> -> expected value 0
<input class="myClass" type="text" /> -> expected value 1
<input class="myClass" type="text" /> -> expected value 2
<input class="myClass" type="hidden" />
<input class="myClass" type="text" /> -> expected value 3
</form>

<form class="myForms">
<input class="myClass" type="text" /> -> expected value 0
<input class="myClass" type="text" /> -> expected value 1
<input class="myClass" type="hidden" />
<input class="myClass" type="text" /> -> expected value 2
<input class="myClass" type="text" /> -> expected value 3
</form>

<form class="myForms">
<input class="myClass" type="text" /> -> expected value 0
<input class="myClass" type="text" /> -> expected value 1
</form>

<form class="myForms">
<input class="myClass" type="text" /> -> expected value 0
<input class="myClass" type="text" /> -> expected value 1
<input class="myClass" type="hidden" />
<input class="myClass" type="hidden" />
<input class="myClass" type="text" /> -> expected value 2
</form>


I tried this Jquery code :

$(document).on("keyup","input[type='text']",function(e){

var elem = $(this);
var theClosest = elem.closest('.myForms');

// And then :
console.log(elem.index(theClosest));

});

// But, I'm wrong : it always returns -1


How could I fix this?

Thank you a lot for your help !

Answer

You need to search index from the text input inside the form, so get the inputs and apply index() method with element as parameter. Also you can use :text pseudo class to select text input which helps to reduce the code.

$(document).on("keyup", ":text", function(e) {
  var elem = $(this),
    form = $(this).closest('.myForms');
  console.log(form.find(':text').index(elem));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="myForms">
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
</form>

<form class="myForms">
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
  <input class="myClass" type="hidden" />
  <input class="myClass" type="text" />
</form>

<form class="myForms">
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
  <input class="myClass" type="hidden" />
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
</form>

<form class="myForms">
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
</form>

<form class="myForms">
  <input class="myClass" type="text" />
  <input class="myClass" type="text" />
  <input class="myClass" type="hidden" />
  <input class="myClass" type="hidden" />
  <input class="myClass" type="text" />
</form>

Comments