jquery - how to make a function live -
i have enter function makes form submit on enter. problem form not exist until click button appends body. there way make $.enter function live? in advance suggestions.
//submit function function submit_chatbox(){ alert('yo'); } $.enter('#message',submit_chatbox); jquery.enter = function(element,callback) { jquery(element).bind('keypress', function(event) { var code=event.charcode || event.keycode; if(code && code == 13) {// if enter pressed callback(event.target); event.preventdefault(); //prevent browser following actual href }; }); };
to make use .live()
, this:
jquery.enter = function(element,callback) { jquery(element).live('keypress', function(event) { var code=event.charcode || event.keycode; if(code && code == 13) {// if enter pressed callback(event.target); event.preventdefault(); //prevent browser following actual href }; }); };
but... have lends self plugin, this:
jquery.fn.enter = function(callback) { return this.live('keypress', function(event) { if(event.which == 13) { callback.call(this, event); event.preventdefault(); }; }); };
then you'd call this:
$('#message').enter(submit_chatbox);
Comments
Post a Comment