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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #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

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Microservices vs Monolithic ArchitectureMiscellaneousWhat’s Interpolation in SCSS (Sass)?SCSSRelative Length Units in CSSCSSWhat is IPS(Intrusion Prevention System), How Does It Work and What are the Detection Types?MiscellaneousHow to add today’s date in HTML input date value using JavaScript?JavaScriptHow to add multiple components in React app?ReactHow to float an image around texts?CSSHow to compress images with gulp in WordPress?WordPressHow to create a for loop in SCSS (Sass)?SCSSHow to show and hide an element on click in React?Reactfirst-of-type and last-of-type selectors in CSSCSSCurrencyPipe in Angular 9 with ExamplesAngular