How to add a class to an element that is outside of your onclick element

324 Views Asked by At

I need some help with some If statement. I have this div

<div id="features-dots">
<ul>
    <a href="#"><li class="one"></li></a>
    <a href="#"><li class="two"></li></a>
    <a href="#"><li class="three"></li></a>
</ul>
</div>

and

<div class="feature_thumb">
<a class="feature_thumb" id="one" onclick="return false;"></a>
</div>
<div class="feature_thumb">
<a class="feature_thumb" id="two" onclick="return false;"></a>
</div>
<div class="feature_thumb">
<a class="feature_thumb" id="three" onclick="return false;"></a>
</div>

and want to add a class to li.one if #one is clicked, to li.two if #two is clicked and so on...Can someone help me with this?

2

There are 2 best solutions below

2
On BEST ANSWER
$('a.feature_thumb').click(function(){
    $('.' + this.id).addClass('your-class');
});

DEMO

2
On

You can use this simple jQuery code:

$('a.feature_thumb').click(function(){
    $('.' + $(this).attr('id')).addClass('your-class');
});

DEMO