tags:

views:

24

answers:

2

On the initial request to a page, I trigger a webservice and want to display a loading message. When the page has finished loading, I want to remove the waiting message from screen, but display it again when a change event in a checkbox triggers an Ajax call with another loading message.

This code is not behaving to the given spec:

$(document).ready(
    $(function() {
     $('#loadingDiv').hide();

      $('#productsDropDown')
         .change(function() {
            var prodValue = $(this).val();
            $('#proddate').load('getpdate.php', {prod: prodValue });
    }); 

    $('#loadingDiv')
        .ajaxStart(function() {
            $(this).show();
        })
        .ajaxStop(function() {
            $(this).hide();
        })    
    ;   
    });
);

what am I doing wrong?

+1  A: 

You need to rearrange your code a bit, like this:

$(function() {
  $('#loadingDiv').hide();
  $('#productsDropDown').change(function() {
    var prodValue = $(this).val();
    $('#proddate').load('getpdate.php', {prod: prodValue });
  }); 

  $('#loadingDiv').ajaxStart(function() {
    $(this).show();
  }).ajaxStop(function() {
    $(this).hide();
  });
});

Currently you have this:

$(document).ready(
    $(function() {
    });
);

document.ready takes a function, you're passing it a jquery element. $(function() { }) is equivalent to wrapping it in $(document).ready(function() { }) already (see details here), no need to wrap it twice.

Nick Craver
thanks. that's the explanation... I got the same result by trial and error
poseid
+1  A: 
$(document).ready(function() {
    var loader = $('#loadingDiv');
    loader.hide();


    $('#productsDropDown').change(function() {
        var prodValue = $(this).val();
        loader.show();
        $('#proddate').load('getpdate.php', {prod: prodValue }, function() {
            loader.hide();
        });
    });
);
Ivo Sabev