How to verify an element exists in the DOM using jQuery? How to verify an element exists in the DOM using jQuery? jquery jquery

How to verify an element exists in the DOM using jQuery?


$ returns an array of matching elements, so check the length property and you're good to go

if ($('#lblUpdateStatus').length) {    $("#lblUpdateStatus").text("");}


The get method returns the matched DOM elements:

if($("#lblUpdateStatus").get(0)){    $("#lblUpdateStatus").click(function () { ... });}

but I am not sure if it is a fast method.


The thing is that the jQuery will only do the requested action if the element exists :-),so you only need:

$("#lblUpdateStatus").text("");