indexing - how to addClass to its equal index .....(JQUERY) -


<ul>    <li>index 0</li>    <li>index 1</li>    <li>index 2</li> </ul> <div>      <a>index 0</a>      <a>index 1 (if click this want addclass li same index of )</a>      <a>index 2</a> </div> 

you can invoke jquerys .index()help method purpose. returns index relative the siblings of current node. find li node according index, use .eq()help

$('a').click(function() {    $('ul li').eq($(this).index()).addclass('your_new_class'); }); 

demo: http://www.jsfiddle.net/2rfn3/

referring comment

$('a').click(function() {    $('ul li').eq($(this).index()).addclass('your_new_class').siblings().removeclass('your_new_class'); }); 

demo: http://www.jsfiddle.net/2rfn3/1/


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -