javascript - Attach unique identifier to object click event with multiple events -
i making minimap game. when click on map, expands larger map. i've added click handler minimap element.
$("#minimap").click(function(){ expand larger map
i've made tooltips tell user click on minimap. when user clicks on minimap, removes tooltip. event handler binding happening within tooltip class:
$("#minimap").on("click", function () { $(this).unbind("click"); removechild(the tooltip object);
i don't want tooltip action continue firing each time click expand map, unbind it. unfortunately, unbinds action expand minimap defined elsewhere.
how can attach sort of unique id tell $("#minimap")
unbind click event associated tooltip?
three things can do:
use
one
instead ofon
, drakes suggested.keep reference function , unbind 1 function later:
function tooltipclickhandler() { $(this).off("click", tooltipclickhandler); removechild(the tooltip object); } $("#minimap").on("click", tooltipclickhandler);
use event namespace, lets uniquely specify your click handler without having remember function reference:
$("#minimap").on("click.tooltiphandler", function () { $(this).off("click.tooltiphandler"); removechild(the tooltip object); });
side note: i've used off
rather unbind
above, because you've used on
set handler , off
"preferred" way of jquery 1.7.
Comments
Post a Comment