I am making a function that needs to use a for loop to output all numbers between two numbers. If the user chooses 1
and 4
, my function would output 1,2,3,4
. Although I have my function working just as I needed it to, the output using document.GetElementById
doesn't want to co-operate (only overwrites final answer(4)), and document.write()
takes me to another page which I don't want. What I need is a method that replaces my p id='replaceOne' which puts all the numbers on the same page. Thank you!
function Counter() {
var number1 = document.getElementById('number1').value;
var number2 = document.getElementById('number2').value;
for (i = number1; i <= number2; i++) {
var answer1 = [i];
document.write(answer1);
}
}
<html>
<head>
<title>
For Loops excercise 1
</title>
</head>
<body>
<table>
<tr>
<td align="left" valign="top">
<p class=headerOne><h3>Counter a</h3>
<textarea id="number1" class="box"></textarea><br>
<textarea id="number2" class="box"></textarea><br>
<button type="button" onclick="Counter()">Find range</button><br>
<p>Your result is: </p>
<p id="replaceOne"></p>
</td>
</body>
</html>