• A
    ...">
    All geek questions in one place

    How to add click event by class name?

    I have an 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 a click event for each menu item by class name?

      $('.menu_button').click(function() { if ( id == "m1" ) .... }) 
    +6
    jquery
    Bdfy Oct 7 '11 at 10:06
    source share
    5 answers

    I would suggest using a live function instead of .click, since elements added at runtime will also be available.

     $('.menu_button').live('click', function() { var id = $(this).attr('id'); if (id == "m1") { //do your stuff here } }); 
    +17
    rlc Oct 7 '11 at 10:25
    source share

    Optimize your code without using live() as we cannot stop the distribution of live() events

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

    The most effective 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 } }); 

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

    +23
    Om shankar May 12, '12 at 18:22
    source share

    You can find the id with this.id

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

    But if the code for each button is completely different, then it may not be practical to use it like that, but instead attach another handler to one identifier.

    +5
    Gaby aka G. petrioli Oct 7 '11 at 10:08
    source share

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

    However, remember that in your example id will be undefined. You will need to use this.id since this will refer to the element that was clicked.

    +1
    James allardice Oct 7 '11 at 10:08
    source share

    If I understand your question correctly, try using this:

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

    Btw: In that case, a switch .. case would be much more suitable!

    0
    janoliver Oct 7 '11 at 10:09
    source share

    Source: https://habr.com/ru/post/898823/

    More articles:

    • Why are some SQLite functions based on zero and some are sqlite
    • Visual Studio Debugging Prevention with Explicit "Unhandled Exception" - multithreading
    • WPF Stack Panel Visibility Animation - visual-studio-2010
    • Remote client computer for debugging - c #
    • How to use members with standard (batch) or private access in REPL? - scala
    • Sqlite3 db lock for file download - c ++
    • How to access a file from the Xcodes Supported Files group in an application? - filesystems
    • SQL Server - creating a schema binding with an indexed view using the current GetDate filter - sql-server
    • PHP Switch with two values ​​- php
    • Multiple background colors on 1 div - html

    All Articles

    Geek Questions | 2019