I'm starting with Javascript, I wrote this function:
function disableField() {
if( document.getElementById("valorFinal").length > 0 ) ) {
document.getElementById("cantidadCopias").disabled = true;
}
}
Which disables the second field named cantidadCopias if the first one is filled.
<label> <span>Valor final:</span>
<input type="text" class="input_text" name="valorFinal" id="valorFinal" onkeydown="disableField()"/>
</label>
<label> <span>Cantidad de Copias:</span>
<input type="text" class="input_text" name="cantidadCopias" id="cantidadCopias"/>
</label>
But it's not disabling the second field when the first one is filled.
It is best if you use
onkeyup()
instead ofonkeydown()
. The problem is the value of the input is not updated on keydown event.Fiddle
javascript
Did you look at the console?
First time you had a spelling error, now your code has an extra
)
Now the next issue is you are not looking at the length of the value.
So the code should look like
but now how it is written, once it is disabled, it will not be re-enabled.
the javascript:
the html:
you should also enable it again when the input length is 0 again.
besides that you should hook onkeyup and not onkeydown.
you can try it here: jsfiddle.net/DBJfN/