2

As opposed to this question, here I am asking if it's possible to do something like this:

$(several different selectors).click(function() {
    console.log("Click");
});

So if I have something like this:

<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>

I want to make several different selectors select #a, #b and #c, and then whichever one I click would log "Click". I tried $('#a', "#b", "#c") and it doesn't work; can someone help?

Community
  • 1
  • 1
Bluefire
  • 13,519
  • 24
  • 74
  • 118

3 Answers3

5

Pass it one string, with the selectors separated by a comma:

$('#a, #b, #c').click(function() {
    console.log("Click");
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
2

you could try this, as i have tried this and it worked for me

    $(document).ready(function () {
        $('#a, #b, #c').click(function () { 
        console.log("Click"); 
      });
    });
Mukesh
  • 41
  • 3
1

Your attempt was very close. Here is the documentation multiple selectors.

Your selector should be $("#a, #b, #c")

Jay Taggart
  • 137
  • 9