I have an click event that I want to assign to more than class. The reason for this is that I'm using this event on different places in my application, and the buttons you click have different styling in the different places.
What I want is something like $('.tag' '.tag2'), which of course don't work.
$('.tag').click(function (){
if ($(this).hasClass('clickedTag')){
// code here
}
else {
// and here
}
});
Have you tried this:
or
or
You can select multiple classes at once with jQuery like this:
Giving a 2nd parameter to the $() function, scopes the selector so
$('.tag', '.tag2')
looks fortag
within elements with the classtag2
.Approach #1
Approach #2
This will also work:
Fiddle
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.
It's like this: