Get value from form using JavaScript.
Using Text and Button object in HTML.
Get value from form using JavaScript.
In script tags we create a function as named calculate in which we pass the form as argument/ reference of form.In block of function we simply use form.result.value=eval(form.entry.value), where result and entry is name of text object and eval is a method that is use to convert the string expression to a numeric value.
when this program is executed in a JavaScript is enabled browser,Enter an expression in the first Text object on the form for evaluation and click the calculate button.
when the button is clicked on its click event is fires, and activate JavaScript event handler onClick. this invokes the function calculate(). this function evaluates the expression in first text object and display the result in last text object on the form.
<html>
<head>
<title>Using text and button object</title>
<script>
function calculate(form)
{
form.result.value=eval(form.entry.value);
}
</script>
</head>
<body bgcolor="cyan">
<form>
Enter mathmatical expression : <br><input type="text" name="entry"><br><br>
<input type="button" onclick="calculate(this.form);" value="calculate"><br><br>
<input type="text" name="result" onfocus="this.blur();">
</form>.
</body>
</html>
Comments
Post a Comment