You start a form in your HTML source using the <FORM> tag.As you might expect, a form ends with the </FORM> tag.Other elements of the user interface are placed between these tags.Interspersed among these tags could be other non-interactive elements of HTML, including tables, emphasized text, font specifications and so on.However, you cannot embed one form within another. It is necessary to assign a name to each form that you create. The name that you assign a form will be used in your JavaScript code to access a modify the elements embedded in the form. You can assign a name to a form by adding the NAME attribute to your form tag and assigning it an appropriate value. Ensure that the name you assign a form conforms to the rules listed for JavaScript variables. You can refer to your form in JavaScript using the notation document.formname. Here is an example of an HTML form,
<FORM NAME=myFirstForm>
<!-- input elements go here -->
</FORM>
When you want to access interface elements in the form above, you would use and expression of the form document.myFirstForm.elementname, where elementname is the name of the element in question.
Listed below are most of the commonly used HTML input elements and their descriptions:
Textbox:
Textbox is one of the most common input elements. Use it when you want a user to enter one line of textual input. The <INPUT> HTML tag, with the TYPE attribute set to TEXT can be used to create textboxes within your HTML forms. Be sure to assign assign each textbox a name using the NAME attribute. This property of the textbox will be used in JavaScript code to access and modify the text that appears in the element. You can access the value in a textbox using the value property. We will use this property in the next section when we discuss event handlers. Set the width of the textbox by assigning a numeric value to the optional SIZE property of the <INPUT> tag. The textbox below is produced using the following lines of HTML:
<INPUT TYPE=TEXT NAME=username SIZE=10>
produces the the following textbox
Note! The value property of a textbox contains a
string
value. If you expect to find a numeric value in a textbox, convert
the string in the value property using the Number() function.
For instance, the following statement takes a string value in a textbox
called ageBox and converts it to an integer:
age = Number ( document.surveyform.ageBox.value )
First tag (for the textbox named "higher") | |
Second tag (for the textbox named "lower") |
Textareas are essentially multi-line textboxes. That is, they can be used to get user input that spans multiple lines, and can be used to display output that appears on multiple lines. Use the <TEXTAREA> tag to create a textarea element on your web page. Terminate the textarea element with a corresponding </TEXTAREA> tag. Any text that appears in between these two tags appears as the default value of this element. As always, use the NAME attribute to specify a name for the textbox. You can also set the number of rows and columns in a textarea element using the ROWS and COLS attributes respectively. When the number of lines in the textarea exceeds the number of rows specified in the ROWS attribute a vertical scroll bar appears at the right margin of the element. In your JavaScript program, use the value property to retrieve or change the text stored in a textarea element. Shown below are the HTML code and the displayed version of a textarea element.
<TEXTAREA NAME=outputbox ROWS=10 COLS=60>This text appears inside the textarea, doesn't it?</TEXTAREA>
Type in the complete tag for a TEXTAREA element named "multiply" |
Almost every form that you create will have one or more buttons (or command buttons) that a user must click in order to initiate computation. You place buttons on your web page using the <INPUT> tag with the TYPE attribute set to BUTTON. You can set the caption to appear on a button using the VALUE tag. The NAME attribute, however, is not very important for button elements. Two special kinds of buttons are especially important: the submit button and the reset button. While the former is not essential in the context of JavaScript programming, the latter is. Use the reset button to clean the contents of a form and reset it to its original state. Shown below are a command button and a reset button, along with the HTML source that created them
<INPUT TYPE=BUTTON VALUE="Click
me for good luck!"><BR>
Type some text into this box and then click on the button below:
<INPUT TYPE=text NAME=junkinput><BR>
<INPUT TYPE=RESET>
produces the following output
|
Try this: In the box below type in the HTML tag for a button captioned "ClickMe!". Then click on the "Show Me!" button to see if you have got it right.