Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

How can I check the existence of an element in jQuery?

The current code that I have is this:

if ($(selector).length > 0) { 

// Do something 

}

Is there a more elegant way to approach this? Perhaps a plugin or a function?

1 Answer

0 votes
by (106k points)
edited by

For checking the existence of an element in jQuery you can use the below mentioned piece of code:

if ($(selector).length)

Want to become a Web Developer, join Web Development Courses now! 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...