1var element = document.getElementById("test");
2 //If it isn't "undefined" and it isn't "null", then it exists.
3if(typeof(element) != 'undefined' && element != null){
4 alert('Element exists');
5} else{
6 alert('Element does not exist');
7}
8
1var myElement = document.getElementById("myElementID");
2
3if(!myElement){
4 //#myElementID element DOES NOT exist
5}
6
7if(myElement){
8 //#myElementID element DOES exists
9}
1<div id="test">Example DIV element.</div>
2
3<script>
4 //Attempt to get the element using document.getElementById
5 var element = document.getElementById("test");
6
7 //If it isn't "undefined" and it isn't "null", then it exists.
8 if(typeof(element) != 'undefined' && element != null){
9 alert('Element exists!');
10 } else{
11 alert('Element does not exist!');
12 }
13</script>
14