Reputation: 3530
I have a few buttons that share the same class name as the divs ids that I would like to fade in.
Example:
<div class="one">button</div>
<div id="one">I am the one div that needs to fade in</div>
<div class="two">button</div>
<div id="two">I am the two div that needs to fade in</div>
Upvotes: 0
Views: 387
Reputation: 2910
You should use a common class/tag for the buttons, and a data-* attribute for the target ID, and retrieve it in your click handler:
HTML:
<div class="button" data-target="one">button</div>
<div id="one">I am the one div that needs to fade in</div>
<div class="button" data-target="two">button</div>
<div id="two">I am the two div that needs to fade in</div>
JS:
$(document).on('click', '.button', function() {
var id = $(this).data('target');
$('#' + id).fadeIn();
});
Upvotes: 1