Linda wolfenden
Linda wolfenden

Reputation: 143

Push user input to array

Hi so yes this is a previous issue i have had, i have hit the books and whent back to basics adjusted a few things but im still having trouble getting the input value to push to the array can some one please help me get my head around this thanks

<!DOCTYPE html>
<html>

 <body>
  <script type="text/javascript">
   var number=["1"]
   function myFunction()
   {
    var x=document.getElementById("box");
     number.push=document.getElementById("input").value;
    x.innerHTML=number.join('<br/>'); 
   }
  </script>
 <form>
  <input id="input" type=text>
   <input type=button onclick="myFunction()" value="Add Number"/>
  </form>

 <div id="box"; style="border:1px solid black;width:150px;height:150px;overflow:auto"> 
  </div>
</body>
</html>

Upvotes: 3

Views: 27179

Answers (2)

Bill
Bill

Reputation: 25555

You were close, here's the working code:

<!DOCTYPE html>
<html>

 <body>
  <script type="text/javascript">
   var number = [];

   function myFunction()
   {
     var x = document.getElementById("box");
     number.push(document.getElementById("input").value);
     x.innerHTML = number.join('<br/>'); 
   }
  </script>
 <form>
  <input id="input" type=text>
   <input type=button onclick="myFunction()" value="Add Number"/>
  </form>

 <div id="box" style="border:1px solid black;width:150px;height:150px;overflow:auto"> 
  </div>
</body>
</html>​

Upvotes: 1

mbinette
mbinette

Reputation: 5094

Here is the mistake:

number.push(document.getElementById("input").value);

push is a method, not an attribute ;-)

JSFiddle

PS: Why the ; after id="box"; ? You should fix that too..! ;-)

Upvotes: 4

Related Questions