function doSomething(){
if ($(this).hasClass('clickedTag')){
// code here
}
else {
// and here
}
}
$('.tag1').click(doSomething);
$('.tag2').click(doSomething);
// or, simplifying further
$(".tag1, .tag2").click(doSomething);
This will also work:
?$(".tag1, .tag2").click(function(){
alert("clicked");
});?
I prefer a separate function (approach #1) if there is a chance that logic will be reused.
See also How can I select an element with multiple classes? for handling multiple classes on the same item.
~ Answered on 2012-05-09 07:49:35