Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select all inputs.value

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();
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can turn the list of nodes into an array, and then use .every():

const allFilled = Array.from(document.querySelectorAll(".input"))
  .every(input => input.value !== "");

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 as inputs[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 simply 3.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading