<ul>
<li>Index 0</li>
<li>Index 1</li>
<li>Index 2</li>
</ul>
<div>
<a>Index 0</a>
<a>Index 1 (If i click this this this i want to addClass to it to LI with the same index of this )</a>
<a>Index 2</a>
</div>
You can invoke jQuerys .index()
help method for that purpose. It returns the index relative the the siblings of the current node. To find the li node
with the according index, use .eq()
help
$('a').click(function() {
$('ul li').eq($(this).index()).addClass('your_new_class');
});
Demo: http://www.jsfiddle.net/2rFn3/
Referring to your 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/
$("a").click(function() {
//Remove classes from other li (per comment)
$("ul li").removeClass("class");
//Now update the clicked item
var length = $(this).prevAll().length;
$("ul li:eq(" + length + ")").addClass("class");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With