Set value to input text in JavaScript

Description

The following code shows how to set value to input text.

Example


<html>
<body>
<script language="JavaScript">
function doMath(){<!--  w w w.  ja v a 2 s . c o m-->
var inputNum=document.form1.input.value;

var result = inputNum;
document.form1.answer.value = result;
}
</script>
<form name=form1>
<input type="text" name="input" size=10>
<input type="button" value="Calculate" onClick='doMath()'>
<br>
Answer:
<input type="text" name="answer" size=10>
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Set value to input text in JavaScript
Home »
  Javascript Tutorial »
    Form »
      Text Input
Javascript Tutorial Text Input
Alert non number form input in JavaScript
Get the input value for input text in JavaS...
Get the value in TextArea in JavaScript
Pass form to a function and display its val...
Set value to input text form control in Jav...
Set value to input text in JavaScript