I wonder if someone could please help me. I'm trying to add a class of 'active' to a div with the same id as a link. When the page loads the first div will be active but I then want to click on a link and add a class of active to a div on the page so I display this div.
HTML:
<ul id="items">
<li>
<a href="#" id="1">item 1</a>
</li>
<li>
<a href="#" id="2">item 2</a>
</li>
<li>
<a href="#" id="3" item 3</a>
</ul>
<div id="product-info">
<div id="1" class="active">
product info
</div>
<div id="2">
product info
</div>
<div id="3">
product info
</div>
</div>
jQuery:
var buttons = $('#items').find('a');
buttons.click(function(){
var id = $(this).attr('id');
var product = $('product-info div');
var productId = product.attr('id');
product.removeClass('active');
}
I'm guessing I need to add an if statement here to say something like if id equal to product id add class
I've tried a few variations but just can't get it. Any help to solve this would be fantastic. If you want to go one step further and suggest a better way I'm all ears.
Thanks in advance