2
I wanted to make a text box run a function when I assign a value to it by code, without user interaction.
I’ve tried the events onChange, onInput, onKeypress, but they all depend on the user’s contact with the text box.
Below is my progress with my doubt, when I press the "Validate" button is externally assigned "123" to a text box and my function ãno identifies the variation of the text box and does not perform the function, it executes only when the user interacts with it.
<form action="" name="f1">
<label>Comparar valores <BR>(Valores iguais = PRETO, Valores diferentes=VERMELHO)</label> <br>
<input type="text" id="pass" oninput="Verifica()"/>
<input type="text" id="pass2" oninput="Verifica()"/>
<script>
function Verifica(){
val1=document.getElementById("pass").value;
val2=document.getElementById("pass2").value;
if(val1!=val2){
document.getElementById("pass").style.borderColor="#f00";
document.getElementById("pass2").style.borderColor="#f00";
}
else{document.getElementById("pass").style.borderColor="#000";
document.getElementById("pass2").style.borderColor="#000";
}
}
</script>
<input type="button" value="Validar" onClick="validarSenha()">
<script>
function validarSenha(){
document.getElementById("pass2").value = "123"
}
</script>
But why don’t you call the function you want in the same code that assigns the value?
– Pablo Almeida
Because this code simulates an external response from an iframe, I can’t change anything from it, I can only get your feedback
– Vitor Marques Lourenço
It seems impossible or impractical to me. See this answer here: http://stackoverflow.com/a/1848008/1796236
– Pablo Almeida
I do not know if what I want to do can be considered an "elegant solution", but I believe it is the only way to get what I need, because I am limited to the server above me, I really want to know if it is possible to do this, thanks for the help
– Vitor Marques Lourenço
Have you tried focusout? https://api.jquery.com/focusout/
– user5988
Have some css solutions or I’m wrong.
– user5988
user5988, The focusout if I’m not mistaken depends on user interaction, and wanted this check of the text box to be done 100% via code
– Vitor Marques Lourenço