1
I need it: if the Code field is empty the Internal Code field can be enabled / empty too, and if the code field is filled the internal code field has to be disabled (because when entering the code the internal code is filled automatically).
But my code is doing backwards, only enables the internal code field after the code is completed. And another, I need to treat the error if the user enters a value in the Code field and remove the value the internal Code field is disabled again immediately, has this possibility ?
<script type="text/javascript">
var codigo = document.getElementById("codigo");
var codigo_interno = document.getElementById("codigo_interno");
codigo.addEventListener('keyup', function() {
codigo_interno.disabled = codigo.value.trim().length > 0;
});
//agora uma função para keyup muito semelhante para o codigo_interno
codigo_interno.addEventListener('keyup', function() {
codigo.disabled = codigo_interno.value.trim().length > 0;
});
</script>
<label>Código</label>
<input type="text" id="codigo">
<label>Código Interno</label>
<input type="text" id="codigo_interno">
parabens, you enabled and disabled, but in case it needs that if one is filled the other disable, ai vc is enabling and disabling only one field
– user28266
@Marcosbrinner That’s not what I understood by the question. Are you saying that if you write in
codigo
disables thecodigo_interno
, but write incodigo_interno
disables thecodigo
that’s it ?– Isac
Correct, he wants if a field regardless of which is written disables the other
– user28266
@Marcosbrinner See my edition and confirm that this is the expected result.
– Isac
Yes, now it’s working as he requested :) I already gave you a up there!
– user28266
with me didn’t work, I edited the code in the question to show how this
– user92870
@Victort. Without the function
libera
. Note that it does not exist in my example, because I do the attribution directly in Javascript– Isac
I’ll edit again, I’m trying without the function on top even
– user92870
@Victort. The problem now has to do with code execution before html was generated. Stackoverflow’s snippet ensures that JS runs after html separates html in the html area and JS in the JS area. In the code you have at this time can solve the problem in one of two ways: 1) Pass the JS to the end of
<body>
. 2) Place the whole JS within a function and call that function in the attributeonload
of<body>
– Isac
thank you worked
– user92870