How can I find out if a given element exists, via field input
?
Example
<html>
<body>
<input type="text" id="txt"><input type="button" value="mais" onclick="busca()">
<pre>Ex.: vaca, boi, porco ou galinha</pre>
<div id='vaca'></div>
<div id='boi'></div>
<div id='porco'></div>
<div id='galinha'></div>
</body>
<script>
function busca() {
var str = document.getElementById('txt').value;
var obj = document.getElementsByTagName('div');
for (var i = 0; i < obj.length; i++) {
var el = o bj[i].id
if (document.getElementById(el) != "undefined") {
alert('existe sim');
break;
} else {
alert('não existe');
break;
}
}
}
</script>
</html>