1
I am trying to validate fields using the Array.prototype.filter() method, but it does not seem to have an effect:
<script>
function isEmpty() {
document.getElementById("submit").disabled =
Array.prototype.filter.call(
Array.prototype.slice.call(
document.getElementsByClassName("input")),
function(input) {
input.value != "";
});
}
</script>
<form>
<input class="input" onkeydown="isEmpty()" onkeyup="isEmpty()">
<input class="input" onkeydown="isEmpty()" onkeyup="isEmpty()">
<input class="input" onkeydown="isEmpty()" onkeyup="isEmpty()">
<input class="input" onkeydown="isEmpty()" onkeyup="isEmpty()">
<button id="submit" disabled>
</form>
What was supposed to occur is the button is enabled only when all fields are filled, but that’s not what happens.
About the Return, I forgot to put here in the post. Already the
.slice
, would not be necessary, since thegetElementsByClassName
returns an array-like and the.filter
works with arrays?– ptkato
@Patrick the
Array.prototype.filter
will treat this list of DOM nodes as an array and hence Slice is not required.– Sergio