3

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>
2
  • Try number.push (document.getElementById("input").value) instead of number.push=document.getElementById("input").value; Commented Sep 26, 2012 at 6:37
  • thank you so much i have been on this for weeks Commented Sep 26, 2012 at 7:10

2 Answers 2

4

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..! ;-)

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

6 Comments

Does that semicolon actually matter with modern, markup-correcting browsers?
It's probably not an issue with most browsers (maybe even all!) but it's still not valid code. ;-)
Going to try with my ancient Safari.
Never said it wouldn't ;-) Lot's of things work but aren't good code! That semicolon doesn't belong there! Thanks for testing tho! Which version?
Version 4.1.3 (4533.19.4). Yes I know, but I was just curious if such a little semicolon matters (in HTML, in JS or most programming languages I know it does).
|
1

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>​

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.