0
I need the for/while to show on the screen the string placed in the input of the first div the first time the loop is executed and the second time to show on the screen the string placed in the input of the second div and so on.
<!DOCTYPE html>
<html>
<body>
<div id="cki11" class="ind">
<input class="in" id="in1" placeholder="Nome do indicado">
</div>
<div id="cki12" class="ind">
<input class="in" id="in1" placeholder="Nome do indicado">
</div>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var i = 1;
var x = 2;
var a = 5;
var e = 5;
while(i <= x){
a = "cki1" + i.toString(); // armazena a string correspondente ao id da div que sera chamada
e = document.getElementById(a).children[0].value; // pega o valor do input com base no div pai
document.write(e+"<br>");// exibe o valor do input
i++;
}
}
</script>
</body>
</html>
I posted an answer, if I have trouble understanding explain how it works;
– Marconi
Lucas notices that
id="in1"
will generate repeated Ids on the page, this is invalid HTML, you must have unique Ids.– Sergio
I hadn’t noticed... Thanks
– lucas pereira