Get value from form using JavaScript.

Using Text and Button object in HTML.

Get  value from form using JavaScript.




In this example we  create a form in which two object Text and Button, as first Text object is used to accept a mathematical expression for evaluation and second Object button is used to calculate the entered expression. When we Click this Button it Calculate the expression in shown in next Text box.
                                      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>








Output
Using text and button object
Enter mathmatical expression :




.

Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

Looping statement in python