admin管理员组

文章数量:1332377

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>

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>
Share Improve this question edited May 4, 2023 at 18:02 RahulOnRails 6,54210 gold badges54 silver badges87 bronze badges asked Sep 7, 2012 at 0:00 Linda wolfendenLinda wolfenden 1432 gold badges3 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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);

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/>'); 
} 

本文标签: javascriptPush integer from Input to arrayStack Overflow