Pass Form value to a function in JavaScript

Description

The following code shows how to pass Form value to a function.

Example


<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
function addThreeNums (inOne, inTwo, inThree) {
var inOne = Number(inOne);<!--   ww w.  ja  v a2s. c o m-->
var inTwo = Number(inTwo);
var inThree = Number(inThree);
return Number(inOne + inTwo + inThree);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="theForm">
<INPUT Type=Text Name="num1">
<INPUT Type=Text Name="num2">
<INPUT Type=Text Name="num3">
<INPUT Type=Button
Value="Add Them"
onClick='document.write("sum:" +addThreeNums(theForm.num1.value,theForm.num2.value,theForm.num3.value));'>
</FORM>
</BODY>
</HTML>

Click to view the demo

The code above generates the following result.

Pass Form value to a function in JavaScript