javascript - If hasClass condition jquery not working properly -
i trying execute alerts onclick events , conditions jquery statement. seems event handlers don't work properly, due fact missing in event handling logic. have 1 button, id #bottone1
, , have menu buttons id #b1
, #b2
.
the first event works fine, adds correctly class "cliccatoinvoca1_onready
". when click on #bottone1
starts the first alert "cliccatoinvoca1_onready
". onclick
event $("#b1")
works properly, removes class "cliccatoinvoca1_onready
" , replaces class "cliccatoinvoca1
".
this point encounter first problem
when click on #bottone1
comes first alert "cliccatoinvoca1_onready
", , "cliccatoinvoca1". when click on #b2
, afer click on #bottone1
executes 3 alerts, "cliccatoinvoca1_onready
", "cliccatoinvoca1
" , "cliccatoinvoca3
".
so, main problem doesn't work if condition execute 1 alert @ time. when click on #bottone1
executes alerts in sequence.
this document.ready function
$(document).ready(function () { $("#select-1").multiselect(); invoca1(); $("#bottone1").addclass("cliccatoinvoca1_onready btn btn-default"); if ($("#bottone1").hasclass("cliccatoinvoca1_onready")) { $("#bottone1").click(function () { alert("cliccatoinvoca1_onready"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); }); } $("#b1").click(function () { invoca1(); $("#bottone1").removeclass("cliccatoinvoca1_onready noclass").addclass("cliccatoinvoca1"); if ($("#bottone1").hasclass("cliccatoinvoca1")) { $("#bottone1").click(function () { alert("cliccatoinvoca1"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); }); } }); $("#b2").click(function () { invoca3(); $("#bottone1").removeclass("cliccatoinvoca1").addclass("cliccatoinvoca3"); if ($("#bottone1").hasclass("cliccatoinvoca3")) { $("#bottone1").click(function () { alert("cliccatoinvoca3"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); }); } }); });
change use delegated event handlers, attached non-changing ancestor element, each matching selector:
e.g. this:
$(document).on('click', "#bottone1.cliccatoinvoca1_onready", function() { alert("cliccatoinvoca1_onready"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); });
the above need 1 click handler, repeat pattern other classes can have. never need hasclass
checks.
your other code becomes simple this:
$("#b1").click(function () { invoca1(); $("#bottone1").removeclass("cliccatoinvoca1_onready noclass").addclass("cliccatoinvoca1"); });
delegated handlers:
- delegated event handlers work listening event (in case
click
) bubble ancestor element. - you choose closest non-changing ancestor element,
document
default if nothing else closer/convenient. not use'body'
has bug related styling can cause mouse events not bubble it. - then applies jquery selector elements in bubble-chain.
- it then applies function, matching elements that caused event.
- the result elements need match @ event time , not event registration time.
this pattern simplify code significantly.
the entire example become this:
$(document).ready(function () { $("#select-1").multiselect(); invoca1(); $("#bottone1").addclass("cliccatoinvoca1_onready btn btn-default"); $("#b1").click(function () { invoca1(); $("#bottone1").removeclass("cliccatoinvoca1_onready noclass").addclass("cliccatoinvoca1"); }); $("#b2").click(function () { invoca3(); $("#bottone1").removeclass("cliccatoinvoca1").addclass("cliccatoinvoca3"); }); $(document).on('click', "#bottone1.cliccatoinvoca1", function () { alert("cliccatoinvoca1"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); }); $(document).on('click', "#bottone1.cliccatoinvoca3", function () { alert("cliccatoinvoca3"); keys = []; $('input[name="multiselect_select-1"]:checked').each(function () { keys.push(this.value); }); }); });
notes:
- i ignoring fact event handlers contain identical code , assume real code has different operations.