proseidon
proseidon

Reputation: 2305

Multiple element selection using AND/OR

I'm unsure of the syntax for multiple selection. What I mean by that is taking your standard click element function:

$("#target").click(function () {});

which means if #target is clicked then do function

and making it mean if #target or #target2 or #target3 is clicked then do function

or if #target1 and #target2 are hovered over, then do function

I know I can just do a click function for each one but that seems like a waste of space, so how would I code that more concisely?

Thanks.

Upvotes: 2

Views: 55

Answers (3)

Codegiant
Codegiant

Reputation: 2150

Try this

$("#target1, #target2, #target3").on('click',
       function () {
          //some code here
        }
);

Multiple selectors are seperated by a ,.

OR

$('[id ^= "target"]').on('click',
        function () {
              //some code here
       }
);

^= means that all ids start with target.

Upvotes: 2

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

just use , between all the selectors you need

$("#target, #target2, #target3").click(function () {});

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219920

Put them all in one selector, separated by a comma:

$("#target1, #target2, #target3").click(function () {});

Upvotes: 5

Related Questions