Assigning more than one class for one event

JqueryClassEvents

Jquery Problem Overview


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
		}
	});
	

Jquery Solutions


Solution 1 - Jquery

Approach #1

function doSomething(){
    if ($(this).hasClass('clickedTag')){
        // code here
    }
    else {
         // and here
    }
}

$('.tag1').click(doSomething);
$('.tag2').click(doSomething);

// or, simplifying further
$(".tag1, .tag2").click(doSomething);

Approach #2

This will also work:

​$(".tag1, .tag2").click(function(){
   alert("clicked");    
});​

Fiddle

I prefer a separate function (approach #1) if there is a chance that logic will be reused.

See also https://stackoverflow.com/questions/1041344/jquery-multiple-class-selector for handling multiple classes on the same item.

Solution 2 - Jquery

You can select multiple classes at once with jQuery like this:

$('.tag, .tag2').click(function() {
    var $this = $(this);
    if ($this.hasClass('tag')) {
        // do stuff
    } else {
        // do other stuff
    }
});

Giving a 2nd parameter to the $() function, scopes the selector so $('.tag', '.tag2') looks for tag within elements with the class tag2.

Solution 3 - Jquery

    $('.tag1, .tag2').on('click', function() {

      if ($(this).hasClass('clickedTag')){
         // code here
      } else {
         // and here
      }

   });

or

function dothing() {
   if ($(this).hasClass('clickedTag')){
        // code here
    } else {
        // and here
   }
}

$('.tag1, .tag2').on('click', dothing);

or

 $('[class^=tag]').on('click', dothing);

Solution 4 - Jquery

It's like this:

$('.tag.clickedTag').click(function (){ 
 // this will catch with two classes
}

$('.tag.clickedTag.otherclass').click(function (){ 
 // this will catch with three classes
}

$('.tag:not(.clickedTag)').click(function (){ 
 // this will catch tag without clickedTag
}

Solution 5 - Jquery

Have you tried this:

 function doSomething() {
     if ($(this).hasClass('clickedTag')){
         // code here
     } else {
         // and here
     }
 }

 $('.tag1, .tag2').click(doSomething);

Solution 6 - Jquery

    $('[class*="tag"]').live('click', function() {
      if ($(this).hasClass('clickedTag')){
         // code here
      } else {
         // and here
      }
   });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionholyredbeardView Question on Stackoverflow
Solution 1 - JqueryTim M.View Answer on Stackoverflow
Solution 2 - JqueryJames SimmView Answer on Stackoverflow
Solution 3 - JquerythecodeparadoxView Answer on Stackoverflow
Solution 4 - JqueryfmsfView Answer on Stackoverflow
Solution 5 - JqueryadrienView Answer on Stackoverflow
Solution 6 - JqueryThulasiramView Answer on Stackoverflow