jquery - Get index of li in multiple parents -


i have loop through elements in each li. i'm trying find index of each el relative group-fields div. have array of el's inside each li tag.

for (var = 0; < arr.length; i++) {       var el = arr[i];       var li = el.closest('li');       console.log( "li index =", li.index() ); } 

the above giving me index relative each parent index of each li inside col-1 , index of each li inside of col-2. want know index of each element in relation top element group-fields.

loose html example:

<div class="group-fields">   <div class="col-1">     <ul>       <li>         <input>el</input>       </li>       <li>         <input>el</input>       </li>       <li>         <div>el</div>       </li>     </ul>   </div>   <div class="col-2">     <ul>       <li>          <div>el</div>       </li>       <li>         <input>el</input>       </li>       <li>         <input>el</input>       </li>     </ul>   </div> </div> 

you can use:

var index = $(li).closest('.group-fields').find('li').index( li ); 

below using click event demonstration purposes only:

$('li').on('click', function() {    //this how to:::    var index = $(this).closest('.group-fields').find('li').index( );    console.log( 'you clicked li index = ' + index );  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div class="group-fields">    <div class="col-1">      <ul>        <li>          <input>el</input>        </li>        <li>          <input>el</input>        </li>        <li>          <div>el</div>        </li>      </ul>    </div>    <div class="col-2">      <ul>        <li>           <div>el</div>        </li>        <li>          <input>el</input>        </li>        <li>          <input>el</input>        </li>      </ul>    </div>  </div>


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -