js check if element exists code example
Example 1: js element exists
var element = document.getElementById("test");
if(typeof(element) != 'undefined' && element != null){
alert('Element exists');
} else{
alert('Element does not exist');
}
Example 2: check if the element exists in javascript
<div id="test">Example DIV element.</div>
<script>
var element = document.getElementById("test");
if(typeof(element) != 'undefined' && element != null){
alert('Element exists!');
} else{
alert('Element does not exist!');
}
</script>
Example 3: js check if dom element exists
var myElement = document.getElementById("myElementID");
if(!myElement){
}
if(myElement){
}
Example 4: check if the element exists in javascript
var myEle = document.getElementById("myElement");
if(myEle){
var myEleValue= myEle.value;
}