jquery - Unbind an event from Document with a different seletor -
i have following global click behaviour in app
$(document).on("click", ".btnsubmit", function (e) { e.preventdefault(); $(this).parents("form").submit(); });
now have one-off instance need .btnsubmit
button on modal window have different behaviour. have 2 workarounds
- using different class
- adding data attribute button , adding conditional binding above
however, i'd prefer remove binding,
$(document).off("click", ".modal.mywindow .btnsubmit")
unfortunately doesn't work, guess because selector different original ".btnsubmit"
is there way unbind event specific selector without affecting other instances of ".btnsubmit"
on same page?
i believe you're correct selectors "off" have match ones given in "on", per jquery docs .off():
to remove specific delegated event handlers, provide selector argument. selector string must match 1 passed .on() when event handler attached.
if added small logic inside of "on", checking whether or not clicked button has .modal.mywindow parent before submitting, satisfactory? like:
$(document).on("click", ".btnsubmit", function (e) { e.preventdefault(); if($(this).closest(".modal.mywindow").length <= 0) { $(this).parents("form").submit(); } });
it's similar option number 2, wouldn't require change html.
Comments
Post a Comment