ifExists

Execute if the element exist

HTML

<button id="start">Start</button>


d,,,,

JavaScript

$.fn.ifExists = function(fn) {
    if (this.length) {
        $(fn(this));
    }
};

$.fn.logger = function(msg) {
    console.log(msg);
    return this;
};

$("#start").ifExists( 
    function($this){
        $this.addClass('aaa').animate({marginTop:20},function(){alert('ok')}).logger('log something');               
    }
);