Search code examples
javascriptjqueryif-statementmethod-chainingchain

jQuery: if inside a chain?


I have this

if(noDelay){
  $(element).find("." + options.class).remove();
} else {
  $(element).find("." + options.class).fadeOut().remove();
}

Is there a way I could avoid repeating the sentence and only add the fadeOut() when a given condition is met?

I can't move fadeOut() till the end of the chain, which probably would've made things easier.

I'm thinking something like

$(element).find("." + options.class).(if(noDelay) fadeOut()).remove();

Thanks in advance, Leo


Solution

  • There is nothing documented like you want but maybe this will work for you:

    $(element).find("." + options.class).fadeOut(noDelay ? 0 : 400).remove();
    

    Why 400, because the default duration for fadeOut is 400 milliseconds. (from documentation)