tags:

views:

175

answers:

4

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.

+5  A: 

Read from this post (courtesy of jakemcgraw):

http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery

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

if ($('#<%=TextBox1.ClientID %>').exists()) {
    // Do something
}
o.k.w
+1 Clearly a preferred way to extend the library.
PHeiberg
+1  A: 

Checking for the selections size() would suffice.

if ($("#<%=TextBox1.ClientID %>").size() > 0) {
   alert("Object is null")
} else {
   alert("Object is exist");
}
Morningcoffee
+1  A: 

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

if ($("#<%=TextBox1.ClientID %>").length == 0){
        // do something here
}
PHeiberg
+3  A: 

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

if ($('#<%=TextBox1.ClientID %>').length) {
    // Do something
}
CMS