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
2 Answers
Reset to default 4You 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
评论列表(0条)