Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking which class was selected in jquery

Tags:

jquery

css

class

If you have an element with two classes applied to it, how can you check what the second class was?

For example:

class="class1 abc"
class="class1 xyz"

When class1 is clicked, how can you check what the second class was, so you can redirect to appropriate action?

 $('.class1').click(function() { 
     // ** var secondClass = abc | xyz
     // ** do something if second class was abc, or something else if second class was xyz **
like image 854
john mossel Avatar asked Apr 21 '11 15:04

john mossel


2 Answers

$('.class1').click(function() {
    if ($(this).hasClass('abc')) {
        //...
    } else {
        //...
    }
});
like image 63
Dustin Laine Avatar answered Oct 21 '22 02:10

Dustin Laine


You could use hasClass()..

$('.class1').click(function() {
    if($this.hasClass("xyz")){
        ...
    } else {
        ...
    }
});
like image 41
Quintin Robinson Avatar answered Oct 21 '22 03:10

Quintin Robinson