8
I’m taking some tests to learn more about callback
in javascript and found the following difficulty:
function nome_existe(element) {
console.log(element.id);
}
window.onload = function() {
Array.from(document.getElementById("form_id")).forEach(function(element, index) {
element.addEventListener("blur", nome_existe(element));
});
};
<form id="form_id" method="POST" action="/cloud/criar-conta.php" autocomplete="off">
<input type="text" name="input_nome" id="input_nome" placeholder="Insira um nome" value="" /><br/>
<input type="text" name="input_email" id="input_email" placeholder="" value="Insira um email" /><br/>
<input type="text" name="input_email_c" id="input_email_c" placeholder="" value="Confirme seu email" /><br/>
<input type="text" name="input_tel" id="input_tel" placeholder="" value="(DDD) - _____ - ____" /><br/>
<input type="password" name="input_senha" id="input_senha" placeholder="Digite uma senha" value="" /><br/>
<input type="password" name="input_senha_c" id="input_senha_c" placeholder="Confirme sua senha" value="" /><br/>
<input type="checkbox" name="input_check" id="input_check" onblur="valida_check()" />
<p id="termo_cad">Concordo com os Termos de Serviçes e Política de Privacidade</p>
<input type="submit" name="sub_btn" id="sub_btn" value="Enviar" />
</form>
The section places the elements of a form
in a array
and theoretically (in my mind) should assign a listner
to the camp that received the event blur
and then log in the id
of the element in the console.
The difficulty is: Why running the event onload
, is logged into the console ids
of all elements, even without the event occurring blur
?
Show what’s going on, I imagine it’s normal, after all it’s adding all of them, the call is something else.
– Maniero
This occurs in all browsers?
– Oralista de Sistemas
What is happening is:
console.log(element.id);
. I have aform
with 8 elements of the typetext
. Then row after row in the console is shown the field id. However I would like this to occur only in the field where there is theblur
.– MagicHat
@Renan yes for Chrome and FF.
– MagicHat
It is well at the discretion of the same developer @Magichat, the way it was was not wrong. I only edited because the visually line where has the
Array
was along with the keys that opens the function and was somewhat confused the analysis. I only used the automatic (Organize) snippet :p identator– BrTkCa
A blz... To tell you the truth I don’t know how six can, indent each language in a style, I’ve given up everything the same... May Even so.. Vlw man...
– MagicHat