Stevik
Stevik

Reputation: 1152

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.

Upvotes: 5

Views: 677

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337713

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

Upvotes: 6

Amir Popovich
Amir Popovich

Reputation: 29846

Simply use a comma to do that:

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

JSFIDDLE.

Upvotes: 5

Related Questions