Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  184] [ 1]  / answers: 1 / hits: 136319  / 11 Years ago, fri, october 25, 2013, 12:00:00

So I have some basic code in html here, i just have two textbox which u can type numbers in and when you click the button, it adds em both up, and in a perfect world, it would display the answer in that third textbox.


<html>
<head>
</head>
<script type="text/javascript">
function myfunction()
{
var first = document.getElementById("textbox1").value;
var second = document.getElementById("textbox2").value;
var answer = +first + +second;
var textbox3 = answer;
}
</script>
<body>
<input type="text" name="textbox1" id="textbox1" />
+
<input type="text" name="textbox2" id="textbox2" />
<input type="submit" name="button" id="button1" onclick="myfunction()" value="=" />

<input type="text" name="textbox3" id="textbox3" readonly="true"/>
<br />
Your answer is: --
</body>
</html>

however, i can't get the answer to display in that textbox3. Does anyone know how to assign a value to that third textbox from a variable?


also, as an added bonus, if anyone knows a way to also make the last line "Your answer is: --" display the answer as well, that would be amazing.


More From » html

 Answers
22



function myfunction() {
var first = document.getElementById(textbox1).value;
var second = document.getElementById(textbox2).value;
var answer = parseFloat(first) + parseFloat(second);

var textbox3 = document.getElementById('textbox3');
textbox3.value = answer;
}

<input type=text name=textbox1 id=textbox1 /> + <input type=text name=textbox2 id=textbox2 />
<input type=submit name=button id=button1 onclick=myfunction() value== />
<br/> Your answer is:--
<input type=text name=textbox3 id=textbox3 readonly=true />




[#74744] Thursday, October 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiley

Total Points: 733
Total Questions: 118
Total Answers: 94

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
;