if element exists code example

Example 1: jquery check if element exists

// Check if an element currently exists
if ($('#element').length) {

}

Example 2: js element exists

var element = document.getElementById("test");
    //If it isn't "undefined" and it isn't "null", then it exists.
if(typeof(element) != 'undefined' && element != null){
    alert('Element exists');
} else{
	alert('Element does not exist');
}

Example 3: check element exist in jquery

if ($('.element').length) {
  // there is at least one element matching the selector
}

Example 4: js check if dom element exists

var myElement = document.getElementById("myElementID");

if(!myElement){
    //#myElementID element DOES NOT exist
}

if(myElement){
    //#myElementID element DOES exists
}

Example 5: check if the element exists in javascript

var myEle = document.getElementById("myElement");
    if(myEle){
        var myEleValue= myEle.value;
    }