arrays - jQuery recursively find element by given index -


i have menu nested unordered lists (ul) , have array contains list items indexes, indexes obtained parent's index. cant add class recursive list items jquery.

for example:

i have array [1,1,0] , array have add class list item contains link link 2, sub link 2 , deep menu 1:

<ul>     <li><a href="">my link 1</a></li>     <li>         <a href="">my link 2</a>         <ul>             <li><a href="">sub link 1</a></li>             <li>                 <a href="">sub link 2</a>                 <ul>                     <li><a href="">deep menu 1</a></li>                     <li><a href="">deep menu 2</a></li>                     <li><a href="">deep menu 3</a></li>                 </ul>             </li>         </ul>     </li>     <li><a href="">my link 3</a></li> </ul> 

i hope can explain problem. english not native language , in native language cant explain problem properly.

from i've understood :

var current = $('div.parent'), // parent of master ul     arr = [1, 1, 0],     yourclass = 'myclass';  (var = 0; < arr.length; i++) {     // add +1 because nth-child 1,n based, not 0,n     current = current.find('>ul>li:nth-child(' + (arr[i] + 1) + ')');     if (current.length === 0) break;      current.addclass(yourclass); } 

this add class <li /> tag, if prefer set <a />, current.find('a').addclass('yourclass'); instead of current.addclass(yourclass);.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -