show the value of the children inputs of divs js

0

I need the for / while to display on the screen the string placed in the input of the first div the first time the loop is executed and the second time it shows 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>
    
asked by anonymous 06.07.2018 / 16:27

1 answer

1

Why not just this?

function myFunction() {
    var inputArray =  document.querySelectorAll(".ind input");
    for(let i = 0; i < inputArray.length; i++){
      document.write(inputArray[i].value + '<br>');
    }
}
<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>
    
06.07.2018 / 16:33