2

I want to control does an element exist in document with its ID, when page is loading. I tried code which is below, but i failed.

   if($(':not(#<%=TextBox1.ClientID %>)')){
     alert("Object is null")else{alert("Object is exist")}}

Thansk for your helps already now.

Kerberos
  • 1,228
  • 6
  • 24
  • 48
  • Thank for your all suggests. I try every suggest in my asp.net project. There is no problem in js side but when i use postback method consist a problem. – Kerberos Oct 31 '09 at 17:25

4 Answers4

9

Read from this post (courtesy of jakemcgraw):

Is there an "exists" function for jQuery?

jQuery.fn.exists = function(){return jQuery(this).length>0;}

if ($('#<%=TextBox1.ClientID %>').exists()) {
    // Do something
}
Community
  • 1
  • 1
o.k.w
  • 25,490
  • 6
  • 66
  • 63
8

I just use directly the length property, as suggested on the jQuery FAQ:

if ($('#<%=TextBox1.ClientID %>').length) {
    // Do something
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
2

The simplest I can think of when using JQuery is this:

if ($("#<%=TextBox1.ClientID %>").length == 0){
        // do something here
}
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
1

Checking for the selections size() would suffice.

if ($("#<%=TextBox1.ClientID %>").size() > 0) {
   alert("Object is null")
} else {
   alert("Object is exist");
}
Martin Nycander
  • 1,309
  • 13
  • 29