3

Hi im trying to get the number pushed into the array but cant seem to link the input to the array any help please

<html>
  <body>
    <form id="myform">
      <input type="text" name="input">
      <button onclick="myFunction()">Add number</button>
    </form>
    <br>
    <div id="box" style="border:1px solid black;width:150px;height:150px;overflow:auto"></div>
    <script>
      var number= [];
      function myFunction(){
        number.push=("myform")
        var x=document.getElementById("box");
        x.innerHTML=number.join('<br/>');
      }
    </script>
  </body>
</html>

2 Answers 2

4

You are assigning, when you should be calling. On top of that you are fetching the value wrong.

number.push(document.getElementById('myform')['input'].value);
Sign up to request clarification or add additional context in comments.

Comments

0

You are adding the id of the form as a string. You have to add the value of the field, so that you can get it later:

var number= []; 
function myFunction() 
{ 
    var input = document.getElementById('input');
    number.push(input.value);

    var x=document.getElementById("box"); 
    x.innerHTML=number.join('<br/>'); 
} 

4 Comments

Actually, this code is assigning the value of the input to the property 'push' on the object 'number'.
Ops... sorry. that was a typo. Thanks.
i don't understand what you say. Actually i have the numbers 1,2,3.. after push to array using "items.push(values) ". its display like this. ['1','2','3,']. But i want [1,2,3] like this how to do
To convert the values to Numbers you will have to call parseInt(value, base) on them (eg.: var num = parseInt(input.value, 10);).

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.