Posts

Showing posts from January 12, 2011

jquery live() and bind() methods

I get asked occasionally when to use the bind () and live () jQuery event methods. bind() will bind a handler to one or more events for each matched element, at the time the call is made; whereas live() uses event delegation to bind a handler to an event for all current and ‘future’ matched elements. If you have controls that aren’t rendered dynamically, use bind(). If you have controls that are rendered dynamically, use live(). Here are two examples. Bind < html xmlns ="http://www.w3.org/1999/xhtml" > < head > < title ></ title > < script language ="javascript" type ="text/javascript" src ="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </ script > < script language ="javascript" type ="text/javascript"> $( function () { $( "input[type=button]" ).bind( "click" , function () { $( this ).after( "<br /><in