JQuery Programmer Question: Download JQuery Programmer PDF

How can events be prevented to work after an ajax request?

Tweet Share WhatsApp

Answer:

here are two ways to handle this issue:

1. Use of event delegation : The event delegation technique works on principle by exploiting the event bubbling. It uses event bubbling to capture the events on elements which are present anywhere in the domain object model. In jquery the user can make use of the live and die methods for the events delegation which contains a subset of event types.

For ex. Handling even delegation, handling of clicks on any <a> element:
$('#mydiv').click(function(e)
{
if( $(e.target).is('a') )
fn.call(e.target,e);
});
$('#mydiv').load('my.html')

2. Event rebinding usage : When this method is used it requires the user to call the bind method and the added new elements.

For ex.
$('a').click(fn);
$('#mydiv').load('my.html',function()
{
$('#mydiv a').click(fn);
});

Download JQuery Programmer PDF Read All 201 JQuery Programmer Questions
Previous QuestionNext Question
How does jquery store data related to an element?Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?