jQuery $(“.class”).click(); - multiple elements, click event once

前端 未结 18 1543
借酒劲吻你
借酒劲吻你 2020-11-29 00:01

I have multiple classes on a page of the same name. I then have a .click() event in my JS. What I want to happen is the click event only happen once, regardless of multiple

相关标签:
18条回答
  • 2020-11-29 00:46

    In this situation I would try:

    $(document).on('click','.addproduct', function(){
    
        //your code here
    
     });
    

    then, if you need to perform something in the other elements with the same class on click on one ot them you can loop through the elements:

    $(document).on('click','.addproduct', function(){
       $('.addproduct').each( function(){
    
          //your code here
         }
      );
     }
    );
    
    0 讨论(0)
  • 2020-11-29 00:48

    $(document).ready(function(){
    
        $(".addproduct").each(function(){
              $(this).unbind().click(function(){
                   console.log('div is clicked, my content => ' + $(this).html());
               });
         });
    }); 
     
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="addproduct 151">product info 1</div>
    <div class="addproduct 151">product info 2</div>
    <div class="addproduct 151">product info 3</div>
    <div class="addproduct 151">product info 4</div>
    <div class="addproduct 151">product info 5</div>

    0 讨论(0)
  • 2020-11-29 00:51

    your event is triggered only once... so this code may work try this

        $(".addproduct,.addproduct,.addproduct,.addproduct,.addproduct").click(function(){//do     something fired 5 times});
    
    0 讨论(0)
  • 2020-11-29 00:53

    Apologies for bumping this old thread. I had the same problem right now, and I wanted to share my solution.

    $(".yourButtonClass").on('click', function(event){
        event.stopPropagation();
        event.stopImmediatePropagation();
        //(... rest of your JS code)
    });
    

    event.StopPropagation and event.StopImmediatePropagation() should do the trick.

    Having a .class selector for Event handler will result in bubbling of click event (sometimes to Parent element, sometimes to Children elements in DOM).

    event.StopPropagation() method ensures that event doesn't bubble to Parent elements, while event.StopImmediatePropagation()method ensures that event doesn't bubble to Children elements of desired class selector.

    Sources: https://api.jquery.com/event.stoppropagation/ https://api.jquery.com/event.stopimmediatepropagation/

    0 讨论(0)
  • 2020-11-29 00:53

    I tried it myself in my project.

    All my rows in a table have a class "contact":

    All my rows in a table have a class "contact".

    My code looks like this:

    var contactManager = {};
    
    contactManager.showEditTools = function(row) {
      console.debug(row);
    }
    
    $('.contact').click(function(event){
      console.log("this should appear just once!");
      alert("I hope this will appear just once!");
      contactManager.showEditTools(event);
    });
    

    And I was first scared to see my whole rows as a result in the Firebug console when I executed the code:

    Firebug console screenshot after code execution

    But then I realized that the click event was not fired, because no alert-dialog appeared. Firebug shows you just the elements which are affected by the click overiding. So there is nothing to worry.

    0 讨论(0)
  • 2020-11-29 00:53

    This question have been resolved and also got a lot of responses, but i want to add something to it. I was trying to figure out, why my click element his firing 77 times, and not one time.

    in my code, i had an each, running a json response and displaying it as divs with buttons. And then i declared the click event inside the each.

    $(data).each(function(){
        button = $('<button>');
        button.addClass('test-button');
        button.appendTo("body");
    
        $('.test-button').click(function(){
            console.log('i was clicked');
        })
    })
    

    If you write your your code like this, the class .test-button will get multiple click events. For example, my data has 77 lines, so the each will run 77 times, that means i will decline the click event on the class 77 times. When you click the element, it will be fired 77 times.

    But if you wirte it like this:

    $(data).each(function(){
        button = $('<button>');
        button.addClass('test-button');
        button.appendTo("body");
    })
    
        $('.test-button').click(function(){
            console.log('i was clicked');
        })
    

    you are declaring the click element after the each. That means, the each will run its 77 times, and the click element will be declared only one time. So, if you click the element, it will be fired only one time.

    0 讨论(0)
提交回复
热议问题