Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  176] [ 1]  / answers: 1 / hits: 17400  / 13 Years ago, sat, june 18, 2011, 12:00:00

hello im new and learning javascript.



I'm trying to make a program of addition through text field.



Check the html code on js fiddle http://jsfiddle.net/fCXMt/



What I need to know is how can I accept user input in text field and diplay output in P tag.



<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>untitled</title>
</head>
<body>
<input id=value1 type=text />
<span> + </span>
<input id=value2 type=text />
<input type=submit onclick=output();>
<p id=result> </p>

<script type=text/javascript language=javascript charset=utf-8>
var value1 = document.getElementById('value1').innerHTML;
var value2 = document.getElementById('value2').innerHTML;

function output(){
document.getElementById('result').innerHTML = value1 + value2;
}

</script>
</body>
</html>

More From » html

 Answers
114

The property for getting the value of a textbox is value, not innerHTML, so change those, and you will also need to use eval or parseInt on the textbox values, otherwise it will concatenate them as strings.



Also, you need to move your variable declarations inside the function, so that when the function is called, the current values from the textboxes are retreived.



See update fiddle here.


[#91645] Thursday, June 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonmicahm

Total Points: 603
Total Questions: 120
Total Answers: 108

Location: Guam
Member since Fri, Jul 31, 2020
4 Years ago
;