jQuery philosophical .is() extention

Written by Alex Wolkov

Love little snipplets that make your life better.

This snipplet by @doublerebel on github, extends the .is() function in jQuery, so that if it’s used without parameters inside, will return if the DOM has been found on page

Consider the following

if($('#somediv').is()) {


} else{

console.log('no ajax placeholder')


The script itself is pretty straightforward, so straightforward in fact, that one want’s this implemented in jQuery core

(function($) { $.fn.extend({ _is: $.fn.is, is: function(s) { return s ? this._is(s) : !!this.length; } }); })(jQuery);

You can fork this on github

  • Isn’t the same of $(“#myDiv”).length == 0 ???

  • Altryne

    Does the same thing, but you can chain it, and you can’t chain an if.
    Plus there already is a is() function , this completes it 🙂

  • Darn, MooTools is so much better at this. if($(‘el’)) {}
    Check out this post: http://frontend.co.il/fastest-way-to-verify-element-existence/

  • Why do we need to relate this method to the existing is()?
    Isn’t it more semantically to call it exist()?

  • Anonymous

    Shlomi this is just a proposition
    Although, read the name of the post, it’s the philosophical if
    it mean that you can as about something is it, and the answer will be yes/no
    Like when you ask if something fits the selector you want, the answer is also yes/no