11

I have a example html menu:

<div class="mmenu">
    <ul>
        <li>
            <div  class="menu_button" id="m1" >A</div>
        </li>
        <li>
            <div  class="menu_button" id="m2" >B</div>
        </li>
        <li>
            <div  class="menu_button" id="m3" >C</div>
    </ul>
</div>

Can I add click event for each element of menu by class name ?

 $('.menu_button').click(function() {
     if ( id == "m1" ) ....
 })
Bdfy
  • 23,141
  • 55
  • 131
  • 179

5 Answers5

29

Optimize your code by not using live() as we cannot stop propagation of live() events

Use on() (jQuery 1.7+) or delegate() (below 1.7)

Most efficient solution for your scenario in this case would be:

//  $('.mmenu').on("click", ".menu_button", function() {   // jQuery 1.7 & up
    $('.mmenu').delegate(".menu_button", "click", function() {
        var id = $(this).attr('id') // or this.id
        if ( id == "m1" ) {
            // ..code
        }
    });

In this way, you have only one click event bound to the main div $('.mmenu'), which will also work if you add elements (new li with divs) in the future

Om Shankar
  • 7,989
  • 4
  • 34
  • 54
20

I would suggest to use the live function, instead of the .click, because the elements added on run-time will also be clickable.

$('.menu_button').live('click', function() {
  var id = $(this).attr('id');
  if (id == "m1") {
      //do your stuff here
  }
});
rlc
  • 5,809
  • 5
  • 38
  • 46
  • 16
    It's now preferable to use `.on()` instead of `.live()`. See other answers. – Luke Aug 12 '15 at 14:04
  • 4
    live() is deprecated since 1.7 and removed since 1.9. See [http://api.jquery.com/live/](http://api.jquery.com/live/) – Virtual Apr 02 '17 at 08:11
3

You can find the id with this.id

$('.menu_button').click(function() {
     if ( this.id == "m1" ) ....
 })

But if the code is completely different for each button, then it may not be useful to use it like that but instead bind a different handler per id.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 1
    A sightly better way is $('.mmenu').delegate('.menu_button','click',function() { if ( this.id == "m1" ) .... }) as it places one click handler at the parent level whcih listens for a click events fired by an element with a class of menu_button. – Alistair Laing Oct 07 '11 at 10:21
  • @Alistair, very true.. but since the OP seems a beginner in jQuery, i wanted to go slow :) – Gabriele Petrioli Oct 07 '11 at 11:41
1

Yes. You can bind a click event handler to any set of DOM elements, whether they're selected by class or anything else. The syntax in your example is correct, and the event handler will be bound to each element matched by the selector.

However, bear in mind that in your example id will be undefined. You would have to use this.id, as this will refer to the element that has been clicked.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
0

If I understand your question correctly, try using this:

$('.menu_button').click(function() {
     if ( $(this).attr('id') == "m1" ) ....
 })

Btw: In this case, a switch .. case would be far more appropriate!

janoliver
  • 7,744
  • 14
  • 60
  • 103