javascript - Push integer from Input to array - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305779a4418907.html

相关推荐

  • javascript - Push integer from Input to array - Stack Overflow

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

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信