Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind event after ajax success in jQuery

Tags:

jquery

ajax

So here is my code:

$(document).ready( function() {
$('#form').bind('change', function(){
    $.ajax({
    type: 'get',
    url: 'api.php',
    data: 'task=getdirs&formname='+$('#form').attr('value'),
    dataType: "text",
    success: function (html){
        $('#chdir').html(html);
        $('#chdir select').bind('change', getDirs());
        }
    });
});
function getDirs(){
}})

#form here has a <select> element. The ajax call returns a piece of html with a new <select> element.
It works nice: in the #chdir div I get a new dropdown element. But the event inside the success part fires only once. Then this event does not work anymore at all.
What can I do to make the newly created <select> element work in the same way as the first?

like image 273
elisium Avatar asked Feb 23 '10 16:02

elisium


People also ask

How do you bind events on ajax loaded content?

The jQuery events (click, change, blur, hover, submit, etc) need to be bind properly in the dynamic content loaded by the Ajax request. You need to use the event delegation for Ajax generated content using jQuery. Use jQuery delegation to attach an event in the dynamically created content by Ajax.

How do you call a function after a success on ajax?

Call function after ajax complete You can simply copy/paste code in the body tag of the html page and reload page to get the content form https://api.joind.in/v2.1/talks/10889 URL after success and call callFunctionAfterAjaxCallComplete() function after ajax request is completed.

What is difference between success and complete in ajax?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.

Is ajax successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.


2 Answers

You are invoking the getDirs function directly on the bind method call, you should only do it if this function returns another function, but I think that's not the case.

Change:

$('#chdir select').bind('change', getDirs());

To:

$('#chdir select').bind('change', getDirs);

Or if you are using jQuery 1.4+, you can bind the change event with the live method only once, and you will not need to re-bind the event after that:

$(document).ready(function () {
  $('#chdir select').live('change', getDirs);
});
like image 85
Christian C. Salvadó Avatar answered Oct 11 '22 13:10

Christian C. Salvadó


Because this SO post came up in my google search, I thought I should mention that .live has been deprecated as of 1.9, and the recommended method is now .on

http://api.jquery.com/on/

like image 31
Dave Baghdanov Avatar answered Oct 11 '22 14:10

Dave Baghdanov