I have multiple class events bind together for a click event ,
i want to know which class is clicked , how can i get current user selected class
$('.class2 , .class3 , .class3').bind('click', function () {
location.href = "test.htm";
});
You can use jQuery to check for classes by name: $(event. target). hasClass('konbo');
Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.
You can use .className
like this:
$('.class2 , .class3, .class3').bind('click', function() {
alert(this.className);
location.href = "test.htm";
});
It can be anywhere from 1 to 3 of those classes though, including other unrelated classes.
Or, since you only actually have 2 if you want to test it using .hasClass()
that's an option as well:
$('.class2 , .class3').bind('click', function() {
var c = $(this).hasClass("class2") ? "class2" : "class3";
alert(c);
location.href = "test.htm";
});
You can use .hasClass()
$('.class1 , .class2 , .class3').bind('click', function() {
if($(this).hasClass('class1')) {
location.href = "test1.htm";
} else if($(this).hasClass('class2')) {
location.href = "test2.htm";
} else if($(this).hasClass('class3')) {
location.href = "test3.htm";
}
});
You could use the hasClass method ala:
if($(this).hasClass('class2'))
Though I'm not sure how many classes you'd look through or what exactly you need it for so it might not be the best route :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With