I have 4 inputs in my form and select them with querySelectorAll. I want to check if the value of all inputs is not empty, how can I do it in an efficient way?
what I did:
const inputs = document.querySelectorAll('.input'):
if (inputs[0,1,2,3].value !== '') {
alert('submit');
form.reset();
}
2
Answers
You can turn the list of nodes into an array, and then use
.every()
:Your attempt:
inputs[0,1,2,3].value
will not work; JavaScript does not provide the implied facility. Instead,inputs[0,1,2,3]
ends up being exactly the same asinputs[3]
, because the commas are parsed as the "comma operator", which is an expression separator. The first three expressions are evaluated but the values are ignored; the overall result is simply3
.inputs[0,1,2,3]
is not the way how you access each element in the array. You need to go every index to obtain the value from the array.Solution:
You can try giving the values to these inputs and check the result.