In this unit, simple examples of using JavaScript to carry out some caclulation are shown.
1. HTML Form: getting input from user
It's possible to include text box, option menus, and toggle buttons in your web page, so that you can accept user input. Here is more details on this: (HTML form).
Your name :<input type = "text" name = "name" id = "name" size = "15" maxlength = "15" /> <input type="button" value="OK" onclick="onOK()" /> <input type="button" value="Cancel" onclick="onCancel()" /> <div id = "greeting"></div>Here is what the above looks like: We need to provide corresponding JavaScript code for action when the button is clicked:
<script type = "text/javascript"> function onOK() { var x = document.getElementById("name").value; var figs = "<font color=orange><b> "+"Hello " + x; figs = figs + "<br></font></b>"; document.getElementById("greeting").innerHTML = figs; } </script>2. Perform arithmetic operations
Once you obtain input from the form (see above), you can perform arithemtic operations on them, and then display result back.
See next part of the tutorial for example.