Beginning JavaScript | 4
|
[previous] [next] |
HTML Elements in Forms
Button Form Elements
I'm starting our look at form elements with the standard button element, as it's probably the most commonly used and is fairly simple. The HTML tag to create a button is the <INPUT> tag. For example, to create a button called myButton, which has the words Click Me on its face, the <INPUT> tag would need to be:
The TYPE attribute is set to button and the VALUE attribute is set to the text we want to appear on the face of the button. We can leave the VALUE attribute off, but we'll end up with a blank button, which will leave our users guessing as to its purpose.
This element creates an associated Button object; in this example it is called myButton. This object has all the common properties and methods described above, including the value property. This allows you to change the text on the button face using JavaScript, though this is probably not something you'll need to do very often. What the button is really all about is the onclick event.
We connect to the button's onclick event handler just as we did for the onclick events of other HTML tags such as the tag. All we need do is to define a function that we want to execute when the button is clicked (say, button_onclick()) and then add the onclick event handler as an attribute of the <INPUT> tag:
In the example below we use the methods described above to record how often a button has been clicked on the button face.
Try It Out  Counting Button Clicks
Save this page as ch6_examp2.htm. If you load this page into your browser, you will see a button with Button clicked 0 times on it. On repeatedly pressing this button, you will see the number of button clicks recorded on the top of the button.
How It Works
We start the script block in the head of the page by defining a global variable, accessible anywhere inside our page, called numberOfClicks. We record the number of times the button has been clicked in this variable, and use this information to update the button's text.
The other piece of code in the script block is the definition of the function myButton_onclick(). This function is connected to the onclick event handler in the <INPUT> tag in the body of the page. This tag is for a button element called myButton, and is contained within a form called form1.
Let's look at the myButton_onclick() function a little more closely. First, the function increments the value of the variable numberOfClicks by one.
Next, we update the text on the button face using the Button object's value property.
The function is specific to this form and button, rather than a generic function we'll be using in other situations. Therefore I've referred to the form and button directly using window.document.form1.myButton. Remember that the window object has a property containing the document object, which itself holds all the elements in a page, including the <FORM> element, and that the button is embedded inside our form.
|
[previous] [next] |
Created: January 29, 2001
Revised: January 29, 2001

Find a programming school near you