Yogesh Chauhan's Blog

How to check if the element exists using JavaScript and jQuery?

in JavaScript & jQuery on September 11, 2020

Using jQuery

We can make use of jQuery ID Selector

The syntax is like:


The ID selector selects a single element with the specified id which is same as JavaScript’s document.getElementById().

if ($('#elementId').length > 0) {
  // element exists.

Using JavaScript

We can use the same ID selector but from pure JavaScript.

Once we have that we can try to see if it’s not undefined or null.

var el =  document.getElementById('elementId');

if (typeof(el) != 'undefined' && el != null)
  // element exists.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
CROSS JOIN in PostgresPostgresFew more :nth-child examples in CSSCSSHow to get the first element with a class name xyz using JavaScript?JavaScriptHow to add a new role in WordPress?WordPressWordPress get_posts ExamplesWordPressWhat are Identifiers in JavaScript?JavaScriptEssential SQL Commands We Need to KnowSQL/MySQLWhat is Conditional Rendering in React?ReactHow to render Lists in React?ReactHow to delete a local and a remote Git branches?MiscellaneousTwo ways we can use colon(:) in Envision BasicEnvision BasicWordPress Plugin: Things to knowWordPress