Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery How to control, does an element exist in document?

Tags:

jquery

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.

like image 320
Kerberos Avatar asked Apr 27 '26 11:04

Kerberos


2 Answers

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
}
like image 134
o.k.w Avatar answered May 01 '26 04:05

o.k.w


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

if ($('#<%=TextBox1.ClientID %>').length) {
    // Do something
}
like image 22
Christian C. Salvadó Avatar answered May 01 '26 03:05

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!