Check If HTML Element Exists jQuery
- Article
- Comment
Introduction
How to Check If HTML Element Exists jQuery or HTML element does not Exist. There are several doubts and clarifications in between these two things, whether a html element exist or another case is, if it does not exist. There is a small thing in between both depending on the needs you may require to check and use it appropriately. Let’s See each one and its place of usage.
- Check Whether Element Exist
- Check Whether Element Does not Exist
There is a small difference in between both things. First we will check Element exist.
Element Exist
There is nothing big or nor complicated it’s our length
property helps us to get the size of an element. size
property is deprecated to use.
So the right choice will be length
property. There is an example for you.
var element = jQuery("#kvcodes_element").length(); if(element > 0 ){ // Element Exist }
Alternatively, you can use it like this
if(jQuery('#kvcodes_element').length > 0){ // element exist }
Here the length of the element will be recorded, if the element really exist on DOM.
Please Remember : For Length checking try to use Id not class, because, the class may reused somewhere.
Element Doesn’t Exist
You can check that function like, if the function does not exist means we may need to do certain things. Let’s do this way.
if(jQuery('#kvcodes_element').length == 0){ // element does not exist }
There is a small difference in it. And Remember, you have to use Id for better results. If you really like my article subscribe my newsletters to receive further updates and informations. Also you can follow me on social sites listed below on my profile informations.