Reputation: 531
I've the following function to toogle a div which is working fine. The problem is that I would like to use the same function to other divs without having to create multiple ids. How can this be done?
This is the code that I'm using:
$('#toggle1').click(function() {
$('.toggle1').toggle();
return false;
});
This is my HTML:
<a href="#" id="toggle1">Simple Div Toggle</a><br /><br />
<div class="toggle1" style="display:none;">Hello</div>
Upvotes: 0
Views: 45
Reputation: 30975
$('[id^=toggle]').on('click', function(){
$('.'+this.id).toggle(); // Thanks to Joe
});
fiddle : http://jsfiddle.net/7EY7F/2/
and with multi div : http://jsfiddle.net/7EY7F/3/
Upvotes: 2