2

Why when I pus the Submit button is undefined all of them?

I want to create an array just by using input fields like this.

Is there a nother way to do this?

I tried to do it with Class name and the result is still undefined

function clicked() {
    var input_value = document.querySelectorAll('#data, #data1, #data2, #data3, #data4').value;
    console.log(input_value)
}

document.getElementById('btn').addEventListener('click', clicked);
<input id="data">
<input id="data1">
<input id="data2">
<input id="data3">
<input id="data4">
<button id="btn">Click me</button>

2 Answers 2

3

querySelectorAll will return a "NodeList", which is similar to an array. NodeLists don't have a value property, so it's returning undefined.

If you want to get the value from each of the input boxes, you'll need to loop through the NodeList and pull the value from each HTML element individually.

function clicked() {
    var nodeList = document.querySelectorAll('#data, #data1, #data2, #data3, #data4');
    for (var i = 0; i < nodeList.length; i++) {
      console.log(nodeList[i].value);
    }
}

document.getElementById('btn').addEventListener('click', clicked);
Sign up to request clarification or add additional context in comments.

1 Comment

I want it to return a pure array in console
0

Change fn to this:

function clicked() {
  var inputs= document.querySelectorAll('#data, #data1, #data2, #data3, #data4');
  inputs.forEach(i => {
    console.log(i.value);
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.