27

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

Say for instance you have the div:

<div id="hello"></div>

And you are dynamically creating a div:

<div id="hello"></div>

Is there a function you can use in Jquery which will check to see if the object with the ID you are trying to create already exists on the page?

Community
  • 1
  • 1
TaylorMac
  • 8,882
  • 21
  • 76
  • 104

2 Answers2

16

For jQuery method you could go with

if($("#selector").length) {
    //object already exists
}
Marek Karbarz
  • 28,956
  • 6
  • 53
  • 73
15
if (document.getElementById('hello')) {
    // yup, already there
}

Or, the jQuery way:

if ($('#hello').length) {
    // yup, already there
}
deceze
  • 510,633
  • 85
  • 743
  • 889