javascript - bind and unbind function with a name -
i need bind , unbind function on click. problem need click event (also 'this' clicked element fine)
function clickelement(e) { [...] //here need event or clicked element }
this works, doesn't have event parameter
$('.clickme').on('click', clickelement)
this works can't unbind specific function
$('.clickme').on('click', function(e){clickelement(e)})
this doesn't work:
$('.clickme').on('click', clickelement(e))
why?
i need use .on instead of .click because later need unbind clickelement , clickelement this:
$('.clickme').off('click', clickelement);
the first choice need
function clickelement(e) { console.log(e, this); } $('.clickme').on('click', clickelement);
the reason why
$('.clickme').on('click', function(e){clickelement(e)}) $('.clickme').off('click', function(e){clickelement(e)})
does not work because new anonymous function , not map 1 used in on(). if need that, have keep reference that.
var myfunc = function(e){clickelement(e); }; $('.clickme').on('click', myfunc ).data("fnc", myfunc);
and remove it
var elem = $('.clickme'); elem.off('click', elem.data("fnc") );
Comments
Post a Comment