Ejemplo 1: existe el elemento js
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');
}
Ejemplo 2: comprobar si el elemento existe en javascript
<div id="test">Example DIV element.</div>
<script>
//Attempt to get the element using document.getElementById
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!');
}
</script>
Ejemplo 3: compruebe si el elemento existe en javascript
var myEle = document.getElementById("myElement");
if(myEle){
var myEleValue= myEle.value;
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)