TedN
TedN

Reputation: 33

Cannot get JQuery callback function to work

I've just started with jquery and am having problems with the following code. On click I would like to run the If Statement first then when complete the fadeToggle of the div (#top). My code isn't working, any help would be appreciated.

$('body').click(function() {
    if ($('#top').css('display') == 'block') {
        $('#bottom').css({'background': "url('image' + n + '.png')"});
    }
    else {
        $('#top').css({'background': "url('image' + n + '.png')"});
    }
    n++, function() {
        $('#top').fadeToggle(2000);
    };
});

Upvotes: 0

Views: 42

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You have an error. It should be:

n++;
//-^ Should be a semi-colon and not comma.

I would propose a better solution for you. You need to use:

$('body').click(function() {
  if ($('#top').is(":visible")) {
    $('#bottom').css({'background': "url('image' + n + '.png')"});
  }
  else {
    $('#top').css({'background': "url('image' + n + '.png')"});
  }
  $('#top').fadeToggle(2000, function () {
    n++;
  });
});

Upvotes: 1

Related Questions