jquery - checkbox onchange firing twice -


i'm attaching onchange event on checkboxes this:

$("input[type='checkbox'][name='auswahl[]']").on("change", function() {     alert($(this).attr("id") + ' ' + $("label[for='" + $(this).attr("id") + "']").text() + $(this).attr("checked")); }); 

the checkboxes this:

<input type="checkbox" name="auswahl[]" id="aus_nunatsiaqnews_ca"><label for="aus_nunatsiaqnews_ca" title="canada">nunatsiaq news</label> 

unfortunately event firing twice. when isolate code given above , put on test page fine , event firing once. need help.

this happens when control "onclick" event nested inside control "onclick" event. clicking 1 of elements triggers events both elements since there no way of knowing of them wanted click. fortunately, can prevented in javascript. in html, pass "$event" parameter function handles click event so:

onclick="myfunc($event)"

and then, in "myfunc" use "stoppropagation" such:

myfunc($event) {    // ... code ... //    $event.stoppropagation();  }


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -