Event not found when binding events using jQuery live() or bind()

Jump to: navigation, search

Due to the way Javascript handles varibale scoping it is necessary to pass the "event" and "this" as arguments when binding events with jQuery. Note that in the event handler it may be necessary to change the name of the variables since "event" and "this" are resevered keywords. In the example below we are adding a mousedown event to every element of a table. On mousedown we report the x,y coordinates of the mouse in addition to the text present in the table cell. Example: $('#mytable td').live('mousedown', function(event){onTDMousedown(event,this);}); function onTDMousedown(e, thisElement) { console.log('this text:' + $(thisElement).text()); console.log('mouse pos:' + e.pageX + ',' + e.pageY); }

See also

Personal tools