jQuery's ajaxStart

Go To StackoverFlow.com

0

I am using such construction:

$('#wait').ajaxStart(function() {
    if ($(this).not(':visible'))
        $(this).showWarning();
}).ajaxComplete(function() {
    if ($(this).is(':visible'))
        $(this).hideWarning();
}).ajaxError(function() {
    alert('Unexpected error...');
});

And I want to disable submit button every time the user clicks on it. In order to make it universal - I want to do it in ajaxStart method, so is there any way to get the button's id which initialized the request? Thank you

2012-04-04 05:52
by nKognito
you can use global variable to hold ID of button and use it in ajaxStart function - Govind KamalaPrakash Malviya 2012-04-04 05:59
Can u pass the button click event to the function - Subodh 2012-04-04 06:01
@sub_stantial: what do you mean? To which function should I pass the button click event - nKognito 2012-04-04 06:03
see my answer below - Subodh 2012-04-04 06:04


1

Give all of your submit button a class, for example: submitBtn, then

$('.submitBtn').ajaxStart(function() {
  $(this).prop('disabled', true);
}).ajaxComplete(function() {
  $(this).prop('disabled', false);
});
2012-04-04 06:16
by xdazz


0

Assuming you can pass the button click event to the function here, you can do it like this :

$('#wait').ajaxSend(function(event) {        //pass event object
  $(event.target).css('disabled','disabled');   //get event target
  if ($(this).not(':visible'))
        $(this).showWarning();
}).ajaxComplete(function() {
    if ($(this).is(':visible'))
        $(this).hideWarning();
}).ajaxError(function() {
    alert('Unexpected error...');
});
2012-04-04 06:02
by Subodh
According to jQuery's manual (http://api.jquery.com/ajaxStart/) the callback function does not receives any parameter - nKognito 2012-04-04 06:05
My mistake, it should be ajaxSend and not ajaxStart. You can pass events to ajaxSend, check it out here http://api.jquery.com/ajaxSend - Subodh 2012-04-04 06:17
Actually, ajaxStart works fine with parameters, but its target field refers to $('#wait') and not to initial butto - nKognito 2012-04-04 06:20
what does event.target returns you - Subodh 2012-04-04 06:27
$('#wait') - the parent div or the source.. I don't know how to call i - nKognito 2012-04-04 10:32
ok let me try something, I will let you know if I find an alternative to achieve this - Subodh 2012-04-04 11:04
I think there is no way to implement it. It is neccessary to add such code to each ajaz request - nKognito 2012-05-22 06:18
Ads