This example shows how JavaScript can be used to respond to user input.
Click here to see the example.
Here is the source code:
<html> <head> </head> <body>Let's use the information entered in a textbox.
Enter your name: <input type="text" id="yourname" value=""/>
<input type="button" value="press Here" onClick="name = document.getElementById('yourname').value; document.write('Your name is ' + name);" />
</body> </html>
The line
<input type="text" id="yourname" value=""/>sets up the input box so that the user can type in some text. The input box is given an id so that we can refer to it later.The input box itself is just an empty box. In order for the person using the form to know what to type in, we have some plain html telling him to enter his/her name.
When the user is done typing in his/her information, he/she would use the button to indicate to the browser that the information is ready to be processed. The onClick action of the button tells the browser what to do. In this example, we have
onClick="name = document.getElementById('yourname').value; document.write('Your name is ' + name);"The browser is directed to obtain the value from the textbox using the javascript function document.getElementById() using the id assigned to the textbox, "yourname". Once the value from the textbox is stored in name, document.write() is used to display the value.