0
I would like to know how to recover the input position with javascript, when it is clicked.
When I click on the first input, it would return the value to me 0; In the second input, the value 1, and so on. The ultimate goal is to be able to execute the code within the if, but only when the input list matches only the user-selected/clicked input.
Follow the code so you can see how I’m doing.
var descendentes = document.querySelectorAll(".form-edit input");
//alert(descendentes.length); //Aqui ele retorna a quantidade de inputs que tenho, que são 3.
for(var i = 0; i < descendentes.length; i++){
descendentes[i].addEventListener("click", function (e){
//alert("Ok!");
for(var i = 0; i < descendentes.length; i++){
var clicado = document.forms[0].elements[i]; //É neta linha que tento retornar o número, mas ele me retorna outra coisa(Ele informa que é um objeto html)
alert(clicado); //No caso aqui, exibiria o número do input clicado
/*if(clicado == i){
alert("Funcinou!");
}*/ //O if, neste caso, caso eu clicar no input que eu desejo, eu entro numa seção de códigos para serem executados.
}
})
}
<form class="form-edit">
<div id="teste">
Nome: <input type="text" name="name" /><br />
Idade: <input type="text" name="age" /><br />
E-mail: <input type="text" name="email" /><br />
Lore: <select>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolor </option>
</select>
</div>
</form>
The other comments above are perfect too, but what I understood was your! Thanks for the help
– Wesley Redfield