1

I'm trying to create a loop that will put values from inputs in an array. The point is that I have 81 inputs and their ids are "cells-[x]".

What I tried to do is

<script type="text/javascript">
    var test = [];
    for(i=0;i<80;i++){
        test[i]=(document.getElementById('cell-[i]').value);
    }
</script>

but it's not working.

B.T.W, I might have made a mistake in the for loop itself, but it is not my point(i'm only a beginner).

6
  • 2
    Your test variable doesn't need an iterator unless you want 81 variables. Do var test = document.getElementById('cell-' + i).value Commented May 12, 2018 at 20:01
  • 1
    You can also use Template Literals implemented into ECMAScript 6 standards. var test = document.getElementById(`cell-${i}`).value Commented May 12, 2018 at 20:03
  • 1
    What happens if you add an 81st element? Most of us think about this the other way around: generically get a list of nodes (HTMLInputElements) and iterate over those. Food for thought. Commented May 12, 2018 at 20:06
  • Use modern JS template strings. You're very close, the syntax just wasn't right: getElementById(cell-${i}) instead. Commented May 12, 2018 at 20:12
  • @Mike'Pomax'Kamermans You have been tricked by SO code recognition. getElementById(`cell-[${i}]`) Commented May 12, 2018 at 20:24

2 Answers 2

2

Two things:

1. Your 'cell-[i]' is all in a string, so each time it thinks you're taking the value: 'cell-[i]'. Replace your parameter in getElementById with:

('cell-[' + i + ']').

2. Try:

test.push(document.getElementById('cell-[' + i + ']').value)

That should do it.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't agree with #2. test[i] will find the index of test and set the value correctly. You do not have to use push. push is mainly good for dynamically appending an element to an array. However, the method OP is using will suffice.
@TyQ. - But the array is blank by default and doesn't have any indices initially, which is why you have to push them. Maybe I'm wrong?
Not necessarily. The array may be blank but JS is nothing like languages such as C++. You don't have to specify what value an array has at a specific index. Try this in Chrome console: var arr = []; arr[15] = 5; console.log(arr[15]);
@TyQ. - Never mind! You're right - I'll modify my response. I didn't know that originally, thanks Ty!
Spent like 5 minutes constantly re-reading my answer double checking everything, gets downvoted, delete. lmao i suck at using this site. Your answer is simple and works just as mine .. should have.. Upvoted. :)
|
0

You can use the addEventListener for the form and call the custom function getValues() which will get the values for all the inputs. You can use the querySelectorAll() to collect all the inputs once regardless of what their id is, and then loop over them using forEach and use .push() to add values to the array that we will use to collect the input values.

See a demo below try adding values to input and hit submit button

//get all the inputs 
var inputs = document.querySelectorAll("input");

// valArray used to collect the values of input
var valArray = [];
//function to get the values of the form inputs
function getValues() {

  //loop the aray using foreach
  inputs.forEach(function(input) {

    //use array.push() to add the values to the custom array of input values
    valArray.push(input.value);

  });

}
//add submit event listner for the form
document.querySelector("#my-form").addEventListener("submit", function(e) {
  e.preventDefault(); //stop form from submitting
  getValues();
  console.log(valArray);
});
<form name="my-form" id="my-form">
  <input>
  <input>
  <input>
  <input>
  <input>
  <input>
  <input>
  <input>
  <input type="submit">
</form>

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.