2
How can I know if a certain 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>
I don’t know why, but this question seems very similar to this one: http://answall.com/questions/180595/comor-buscar-um-elemento-no-xml-usando-javascript
– MarceloBoni