JQuery and .length>0

coding-stylejquery

var sd = jQuery(".bla");
if (sd.length>0){

}

Is there any cooler way to write that code? I want some code to execute only if there are some classes named bla. I do know I can use the each, but I do not want to and I do know I can do stuff like sd.action(), but I do not want to.

Best Answer

I'm not sure how much 'cooler' this is, but just:

if ($('.bla').length) {
    // Do stuff
}

Will do the trick.

Edit: If you need to re-use the selector after the check, you can still use the same syntax:

var sd = $(".bla");

if (sd.length) {
    // Do stuff
}