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,190
  • 6
  • 22
  • 46
  • 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
  • 24,849
  • 6
  • 63
  • 62
8

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

if ($('#<%=TextBox1.ClientID %>').length) {
    // Do something
}
Christian C. Salvadó
  • 769,263
  • 179
  • 909
  • 832
2

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

if ($("#<%=TextBox1.ClientID %>").length == 0){
        // do something here
}
PHeiberg
  • 28,915
  • 6
  • 55
  • 79
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,299
  • 13
  • 28