Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Multiple selector

$(document).on('click', '.SELECTOR1 OR #SELECTOR2', function(){
        // some code
});

What I want to achieve is to run some code if either one of the elements is clicked.

like image 913
Stevik Avatar asked Apr 15 '15 15:04

Stevik


2 Answers

The Sizzle selector engine that jQuery uses follows the same rules as CSS. With that in mind you can separate selectors using ,:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
    // some code
});
like image 120
Rory McCrossan Avatar answered Oct 20 '22 18:10

Rory McCrossan


Simply use a comma to do that:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
        // some code
});

JSFIDDLE.

like image 20
Amir Popovich Avatar answered Oct 20 '22 18:10

Amir Popovich