3

Possible Duplicate:
Is there an “exists” function for jQuery

Is there a better way to check if the element is already inserted on page? I'm currently doing this and is working fine, but doesn't seem for me to be the best/fast way

$(element).closest('body').length > 0

Thanks.

Edit:

I doesn't need to know if my element exists, I need to know if it's on page.

Community
  • 1
  • 1
DiogoDoreto
  • 883
  • 4
  • 16

4 Answers4

3

You can use jQuery's built in .has function so your code should look something like:

$('body').has(element)
Gerard
  • 4,818
  • 5
  • 51
  • 80
1

Try this :

$(element, 'body').size() > 0

It will fetch for the element within the body.

Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
0

What you are doing is searching for element "body". It's not what you are looking for

$(element).length > 0

check whether an element exists

Thanh Trung
  • 3,566
  • 3
  • 31
  • 42
0
if ( $('element-selector-here').length )
{
    // element exist
}
else 
{
    // element doesn't exist
}
Jose Faeti
  • 12,126
  • 5
  • 38
  • 52