pgill pgill - 2 months ago 11
Javascript Question

How to implement same set of JavaScript/jQuery functions on multiple elements

I want to run same method for multiple div elements. the code is give here:

<div class="pages" id="oddPage">
<img>
</div>
<div class="pages" id="evenPage">
<img>
</div>


and JS code is:

function flip() {
//Something on odd page;
//Something on even page;
}
function flop() {
//Something on odd page;
// Something on even page;
}


My requirement is is I click on odd page flip() is called and if I click on even page odd function is called.

I do not know how to do it.

M B M B
Answer
$('#oddPage').click(flip);
$('#evenPage').click(flop);

if there are multiple elements with this id, change it to a class because ids are unique and reference it with a .

$('.oddPage').click(flip);
$('.evenPage').click(flop);