user2360754
user2360754

Reputation: 59

jQuery function triggers twice

I have this code http://jsfiddle.net/8dFVU/ and not sure why it triggers twice when you click on the "show price" I guess it is because the checkbox and label but, why? Thanks.

here is the code

(function($){ 
        $(".selector").on('click', function() { 
            console.log(  $(this) );  
            $("#result").val( $("#result").val() +"triggered" );
        });
})(jQuery);

Upvotes: 0

Views: 55

Answers (2)

jacouh
jacouh

Reputation: 8741

Change function to:

(function($){ 
    $(".selector").on('click', function() { 
        console.log(  $(this) );  
        $("#result").val( $("#result").val() +"triggered" );
        return false;
    });
})(jQuery);

As both div and checkbox will trigger click event.

Upvotes: 1

SLaks
SLaks

Reputation: 887275

Clicking on a <label> will also generate a click event for its corresponding control.

Your click handler on the parent element sees both of these click events – the real one from the label and the generated one from the checkbox.

Upvotes: 1

Related Questions