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:

  1. use one instead of on, drakes suggested.

  2. keep reference function , unbind 1 function later:

    function tooltipclickhandler() {     $(this).off("click", tooltipclickhandler);     removechild(the tooltip object); } $("#minimap").on("click", tooltipclickhandler); 
  3. 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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -