Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Typically in JavaScript I do something like the below to verify an element does exist:

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

But, using jQuery - how can I do the same type of thing?

like image 595
Toran Billups Avatar asked Jun 22 '09 13:06

Toran Billups


People also ask

How do you verify element is present in DOM in jQuery give code?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements.

How do you check class is exists in DOM jQuery?

Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not.

How do you check element is exists or not?

Answer: Use the jQuery . length Propertylength property to determine whether an element exists or not in case if you want to fire some event only if a particular element exists in DOM. Here's an example that displays an alert on button click if the specified element exists.


Video Answer


1 Answers

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

if ($('#lblUpdateStatus').length) {     $("#lblUpdateStatus").text(""); } 
like image 115
Dan F Avatar answered Sep 21 '22 03:09

Dan F