0

I am creating 8 dynamic input boxes at max, one at a time on click. I want to store their values in an array in such a way that if I have added 4 button, store 4 values into array and display them in alert, if I have added 5 values, store 5 values into array and display them in alert etc. What I have done so far:

Javascript:

function show(){
  var data = [];
  for(fieldValue=1;fieldValue<=8;fieldValue++){
    var input = document.getElementById('input - '+fieldValue).value;
    data.push(input);
  }
 alert(data);
}

It only alerts the values of inputboxes If I have added all 8 input boxes, otherwise it says " Cannot read property 'value' of null ". Edit: I am giving IDs to theses inputboxes as (partial):

  var inputField = document.createElement('INPUT');
  inputField.id = 'input - '+fieldValue;

  fieldValue++;

It set IDs as input - 1, input - 2 and so on.. Thanks in advance.

4
  • 5
    I don't know if what you're showing is the working code or broken code, but either way, you need to provide a full example that shows the HTML too. Commented Aug 30, 2017 at 12:56
  • When you create inputs, add a unique class and then fetch elements using document.getElementsByClassName(uniqueClassName) and in loop, check for length of returned elements instead of 8 Commented Aug 30, 2017 at 12:56
  • 1
    Instead of using 8 in the loop, you should be checking how many inputs you've added. If you make the added elements a particular class then you can use document.getElementsByClassname() to get the list of inputs directly. Commented Aug 30, 2017 at 12:58
  • @spanky It is valid and running. Also there is no need to add full code, if the problem only exists in a single function :) Commented Aug 30, 2017 at 13:58

3 Answers 3

2

You are looping from 1 to 8.

If you added less than 8 input boxes (let's say 5), at some point document.getElementById('input - '+fieldValue) will return null because there won't be an element with the id 'input - 6' in the document.

To secure your code, you have to check if document.getElementById('input - '+fieldValue) is not null before trying to retrieve the data.

function show(){
  var data = [];
  for(fieldValue=1;fieldValue<=8;fieldValue++){
    var input = document.getElementById('input - '+fieldValue);
    if (input != null) 
       data.push(input.value);
  }
 alert(data);
}
Sign up to request clarification or add additional context in comments.

Comments

0

As people have mentioned you are iterating 8 times even if there are not 8 input elements. It can be much easier using a class selector and jQuery and iterating for each element that shares that class.

function show(){
    var data = [];
    $.each($('.input-item'), function(key, val){
        data.push($(val).val());
    })
   alert(data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <input type="text" id="input-1" class="input-item" />
  <input type="text" id="input-2" class="input-item" />
  <input type="text" id="input-3" class="input-item" />
  <input type="text" id="input-4" class="input-item" />
  <input type="text" id="input-5" class="input-item" />
  <input type="text" id="input-6" class="input-item" />
  <input type="text" id="input-7" class="input-item" />
  <input type="text" id="input-8" class="input-item" />
  <button type="button" onclick="show()">Click me</button>
</form>

2 Comments

Javascript is the basic requirement for my project, Thank you for your answer :)
@HaseebHassy glad to help!
-1
function show() {
  var data = [];
  for (fieldValue = 1; fieldValue <= 8; fieldValue++) {
    var input = document.getElementById('input - ' + fieldValue);
    if (inpt) {
      input = input.value;
      data.push(input);
    }

  }
  alert(data);
}

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.