nkcmr
nkcmr

Reputation: 11010

execute callback even if jquery statement fails

I have a jquery statement as follows:

$('.panel.col2, .panel.col3').fadeOut('fast', function(){
    //SOME CODE
});

The problem is that these elements do not always exist, and I need it to execute the callback regardless of the success of the statement the callback belongs to. Is there any workaround to this?

Upvotes: 0

Views: 161

Answers (2)

avall
avall

Reputation: 2055

Try something like this:

function myCallback(){
   //SOME CODE
}

if($('.panel.col2, .panel.col3').length>0){

   $('.panel.col2, .panel.col3').fadeOut('fast', function(){
       myCallback();
   });
} else {
   myCallback();
}

Upvotes: 1

John Hartsock
John Hartsock

Reputation: 86892

It sounds like you want some code to run in the call back for fadeOut and outside if their are no elements to fade out. Here is an idea but I do not know the context of what you are trying to do.

$(document).ready (function () {
  var panelElement = $('.panel.col2, .panel.col3');
  var fadeOutCallBack = function(){
    //SOME CODE
  };
  panelElement.fadeOut('fast', fadeOutCallBack);
  if (panelElement.length == 0) {
    fadeOutCallBack();
  }
});

Upvotes: 1

Related Questions