1

I am trying to have the NumberBox's onEnter() function to be able to dynamically change the formBox.

If someone types 3 as the input value, I need it to show 3 forms with the exact input boxes. I know how to get the value from the input box and show the amount of forms below.

My problem is that I cannot figure out how to have one submit button store all of the values into an array, such as [[1,1,1,1,1],[2,2,2,2,2],etc].

Once I can figure this out, I should be able to output those values on my own. End goal is to use those values to show the list multiple times on another page. I would prefer to keep this completely in Javascript. I believe it can be done, but I have hit a block and need some help.

function clickMe() {
  var input1 = document.forms["formBox"]["info1"].value;
  var input2 = document.forms["formBox"]["info2"].value;
  var input3 = document.forms["formBox"]["info3"].value;
  var input4 = document.forms["formBox"]["info4"].value;
  var input5 = document.forms["formBox"]["info5"].value;
  var inputArr = [input1,input2,input3,input4,input5];

  document.getElementById("info1").innerHTML = inputArr[0];
  document.getElementById("info2").innerHTML = inputArr[1];
  document.getElementById("info3").innerHTML = inputArr[2];
  document.getElementById("info4").innerHTML = inputArr[3];
  document.getElementById("info5").innerHTML = inputArr[4];
  console.log(inputArr);
}
function onEnter() {
  var qNumber = document.getElementsByName("numberBox")[0].value;
  if(event.keyCode == 13) {
      var qID = document.getElementById("numBox");
      var submitBtn = document.getElementById("submitButton");
      var a = qNumber - 1;
      var b = 0;
      while (b < a) {
          var formClone = document.getElementById("formBox");
          var listClone = formClone.cloneNode(true);
          var text =b+2;
          listClone.id = "formBox" + text;
          console.log(listClone.id);
          document.getElementById("forms").append(listClone);
          b++;
          console.log(b);
      }
      return qID.parentNode.removeChild(qID);
  }
  return qNumber;
}
input{
    display: block;
}
<div id="forms">
    <span id="numBox">
    <label for="numberBox">Number of Forms</label>
    <input type="number" name="numberBox" onkeydown="onEnter()" />
    </span>
    <form id="formBox" name="formBox" action="#" onsubmit="return false;">
        <label for="info1">Input 1:</label>
        <input type="text" name="info1" />
        <label for="info2">Input 2:
        </label>
        <input type="text" name="info2" />
        <label for="info3">Input 3:
        </label>
        <input type="text" name="info3" />
        <label for="info4">Input 4:
        </label>
        <input type="text" name="info4" />
        <label for="info5">Input 5:
        </label>
        <input type="text" name="info5" />
    </form>
</div>
<input type="submit" value="Submit" id="submitButton" onclick="clickMe()" />

<div id="content">
    <span id="info1">input1</span>
    <br/>
    <span id="info2">input2</span>
    <br/>
    <span id="info3">input3</span>
    <br/>
    <span id="info4">input4</span>
    <br/>
    <span id="info5">input5</span>
</div>

2
  • 1
    if you don't know how many inputs user will enter but you need to make an array with all input values you need to select all inputs based on id/className and create a loop that will go through selected elements adding each selected element value to the new created array with push() method. Commented Nov 9, 2019 at 23:18
  • @metamorph_online I have tried the push() method, but I am only able to get the original form values. I have not been able to get form 2, 3, etc values into an array. Commented Nov 10, 2019 at 1:34

2 Answers 2

2

You can query all input elements within form, map their values to another array.

[...document.forms["formBox"].getElementsByTagName("input")].map(input => input.value)
Sign up to request clarification or add additional context in comments.

1 Comment

I am not too familiar with the map method. I have been trying some different things, but still getting stuck. I am assuming this is going to replace my original variables and then pull directly from here when doing my output. My problem right now is that it is not pulling the values from the newly created form. I can only push the values of the original form. I have also gone back and checked without adding a new id to the form, such as the "formBox" + text that I had been using.
0

I was able to loop through all of the inputs, thanks @mike-ezzati and @metamorph_online for pointing me in the right direction. This is my way of doing so, and I was realizing that I needed to bring over the value from the onEnter() function. I ended up using localStorage in order to accomplish this easiest.

    function clickMe() {
        var q = localStorage.getItem("qNumber");
        console.log(q);
        var inputNow = [];
        var allInputs = [];
        var inputNow = document.getElementsByTagName("input");
        for(x=0; x < inputNow.length; x++) {
            allInputs.push(inputNow[x].value);
             console.log(allInputs);
        }
    localStorage.clear();
    }

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.