You can also do:

if ($('#my-element')[0]) {
}

or

if ($('#my-element').size()) {
}

or if you want the sugary syntax you can declare it yourself:

jQuery.fn.exists = jQuery.fn.size;


Mike


On 7/9/07, Felix Geisendörfer <[EMAIL PROTECTED]> wrote:

 I've just been wondering if jQuery has some syntactic sugar for checking if
an element exists. I know the following works:
 --------------------------------------------------------
 if ($('#my-element').length) {
      // #my-element exists
 }
 --------------------------------------------------------
 but is there also something similar to the following?:
 --------------------------------------------------------
 if ($('#my-element').exists()) {
      // #my-element exists
 }
 --------------------------------------------------------
 I'm asking because I know this was one of the things I was initially unsure
about when learning jQuery and I just had a one of my many jQuery converted
friends ask me about it again. So is anybody else thinking some syntactic
sugar could be helpful here? Or what do you think about:
 --------------------------------------------------------
 $('#my-element').is('*') ?
 --------------------------------------------------------
 (Besides that it's probably inefficient *g*)

 -- Felix

--------------------------
 My Blog: http://www.thinkingphp.org
 My Business: http://www.fg-webdesign.de

Reply via email to