jQuery - jQuery check if dom element exists

The jQuery object $("#myid") always returns something - a jQuery object. To check if it actually represents an excisting DOM element on the page you can use the following:

if ( $("#myid").length > 0 ) {
//do something
}
Reply:
 
 
 
 
exactly what I needed. Thanks
Very nice! small piece of script which solves big problems! now using at http://www.livepage.info
You should use .length() ( or .size() ) method instead of .length attribute because it's a jQuery method.

I had some issue using .length attribute, but I can't remember which one !
Thanks..
Thanks a lot for the quick tip!
dzsííír
excelent
Thanks for the tip
Very nice trick ! Thumbs UP !!!!
Thank you. Your hint was useful 2 year after you've posted. I would never think in checking the object's length.
perfect, exactly what I needed, thanks!
rendered @ Fri Mar 28 11:23:37 CET 2014