HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
BEGINNING JAVASCRIPT PART 2 - HTML ELEMENTS IN FORMS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

There are about ten elements commonly found within <FORM> elements. The most useful are shown below, ordered into general types. We give each its name and, in parentheses, the HTML needed to create it, though note this is not the full HTML but only a portion.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book Beginning JavaScript.


As you can see, most of the form elements are created using the <INPUT> tag. One of the <INPUT> tag's attributes is the TYPE attribute. It's this attribute that decides which of the HTML elements this will be. Examples of values for this attribute include button, to create a button, and text to create a text box.

Each form element inside the web page is made available to us as, yes, you guessed it, an object. As with all the other objects we have seen, each element's object has its own set of distinctive properties, methods and events. We'll be taking a look at each form element in turn and how to use its particular properties, methods, and events. But, before we do that, let's look at properties and methods that the objects of the form elements have in common.

Common Properties and Methods

One property that all the objects of the form elements have in common is the name property. We can use the value of this property to reference that particular element in our script. Also, if we are sending the information in the form to a server, then the element's name property is sent along with any value of the form element, so that the server knows what the value relates to.

Most form element objects also have the value property in common, which returns the value of the element. For example, for a text box the value property returns the text that the user has entered in the text box. Also, by setting the value of the value property, it allows us to put text inside the text box. However, the use of the value property is specific to each element, so we'll look at what it means as we look at each individual element below.

All form element objects also have the form property, which returns the Form object in which the element is contained. This can be useful in cases where you have a generic routine that checks the validity of data in a form. For example, when the user clicks a submit button, we can pass the Form object referenced by the form property to our data checker which can use it to loop through each element on the form in turn, checking that data in the element is valid. This is handy if you have more than one form defined on the page or where you have a generic data checker that you cut and paste to different pages – this way you don't need to know the form's name in advance.

Sometimes it's useful to know what type of element you're dealing with, particularly where you're looping through the elements in a form using the elements[] array property of the Form object.

This information can be retrieved using the type property, which each element's object has. This property returns the type of the element, for example button or text.

All form element objects also have the focus() and blur() methods. Focus is a concept you might not have come across yet. If an element is the center of the focus then any key presses made by the user will be passed directly to that element. For example, if a text box has focus, then hitting keys will enter values into the text box. Also, if a button has the focus, then hitting the enter key will cause the button's onclick event handler code to fire, just as if a user had clicked the button with their mouse.

The user can set which element currently has the focus by clicking on it or using the tab key to select it. However we, as the programmer, can also decide which element has the focus by using the form element's object's focus() method. For example, if we have a text box for the user's age to be put into and the user enters an invalid value, such as a letter rather than a number, then we can tell them that their input is invalid and send them back to that text box to correct their mistake.

Blur, which perhaps could be better named 'lost focus', is the opposite of focus. If we want to remove a form element from being the focus of the user's attention, then we can use the blur() method. The blur() method, when used with a form element, usually results in the focus shifting to the containing window.

As well as the focus() and blur() methods, all the form element's objects have the onfocus and onblur event handlers. These are fired, as you'd expect, when an element gets the focus or loses the focus, due to user action or the focus() and blur() methods. The onblur event handler can be a good place to check the validity of data in the element that has just lost the focus. If invalid you can set the focus back to the element and let the user know why the data they entered is wrong.

One thing to be careful of is using the focus() or blur() methods in the onfocus or onblur event handler code. There is a danger of an infinite loop occurring. For example, consider two elements, each of whose onfocus events passes the focus to the other element. Then, if one element gets the focus, its onfocus event will pass the focus to the second element, whose onfocus event will pass the focus back to the first element, and so on until the only way out is to close the browser down. This is not likely to please your users!

Also be very wary of using the focus() and blur() methods to put focus back in a problem field if that field or others depend on some of the user's input. For example, say we have two text boxes, one in which we want the user to enter their city and the other in which we want them to enter their state. Also say that the input into the state text box is checked to make sure that the specified city is in that state. If the state does not contain the city, we put the focus back on the state text box so that the user can change the name of the state. However, if the user actually input the wrong city name and the right state name, they may not be able to go back to the city text box to rectify the problem.

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:

<INPUT TYPE="button" NAME="myButton" VALUE="Click Me">

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 <A> 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:

<INPUT TYPE="button" onclick="button_onclick()">

Try It Out - Counting Button Clicks

In the example below we use the methods described above to record how often a button has been clicked on the button face.

<HTML>
<HEAD>

<SCRIPT LANGUAGE=JavaScript>

var numberOfClicks = 0;

function myButton_onclick()
{
  numberOfClicks++;
  window.document.form1.myButton.value = 'Button clicked ' +
  numberOfClicks + ' times';
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1>
  <INPUT TYPE='button' NAME='myButton' VALUE='Button clicked 0 times' 
  onclick="myButton_onclick()">
</FORM>
</BODY>
</HTML>

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.

<FORM NAME=form1>
  <INPUT TYPE='button' NAME='myButton' VALUE='Button clicked 0 times' 
  onclick="myButton_onclick()">
</FORM>

Let's look at the myButton_onclick() function a little more closely. First, the function increments the value of the variable numberOfClicks by one.

function myButton_onclick()
{
  numberOfClicks++;

Next, we update the text on the button face using the Button object's value property.

  window.document.form1.myButton.value = 'Button clicked ' + 
  numberOfClicks + ' times';
}

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

element, and that the button is embedded inside our form.

Try It Out - onmouseup and onmousedown

Two less commonly used events supported by the Button object in version 4 or higher browsers are the onmousedown and onmouseup events. You can see these two events in action in the next example.
 
<HTML>
<HEAD>

<SCRIPT LANGUAGE=JavaScript>

function myButton_onmouseup() 
{
  document.form1.myButton.value = "Mouse Goes Up"
}

function myButton_onmousedown() 
{
  document.form1.myButton.value = "Mouse Goes Down"
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1>
  <INPUT TYPE='button' NAME='myButton' VALUE=' Mouse Goes Up ' 
  onmouseup="myButton_onmouseup()"
  onmousedown="myButton_onmousedown()">
</FORM>
</BODY>
</HTML>

Save this page as ch6_examp3.htm and load it into your browser. If you click the button with your left mouse button and keep it held down, you'll see the text on the button change to "Mouse Goes Down". As soon as you release the button, the text changes to "Mouse Goes Up".

How It Works

In the body of the page we define a button called myButton within a form called form1. Within the attributes of the <INPUT> tag, we attach the function myButton_onmouseup() to the onmouseup event handler, and the function myButton_onmousedown() to the onmousedown event handler.

<FORM NAME=form1>
  <INPUT TYPE='button' NAME='myButton' VALUE=' Mouse Goes Up ' 
  onmouseup="myButton_onmouseup()"
  onmousedown="myButton_onmousedown()">
</FORM>

The myButton_onmouseup() and myButton_onmousedown() functions are defined in a script block in the head of the page. Each function consists of just a single line of code, in which we use the value property of the Button object to change the text that is displayed on the button's face.

An important point to note is that events like onmouseup and onmousedown only trigger when the mouse pointer is actually over the element in question. For example if you click and keep held down the mouse button over our button, then move the mouse away from the button before releasing the mouse button, you'll find that the onmouseup event does not fire and the text on the button's face does not change. In this instance it would be the document object's onmouseup event handler code that would fire, if we'd connected any code to it.

Don't forget that, like all form element objects, the Button object also has the onfocus and onblur events, though they are rarely used in the context of buttons.

The Submit and Reset Buttons

Two additional types of button are the submit and reset buttons. Defining the submit and reset buttons is done in the same way as defining a standard button, except that the TYPE attribute of the <INPUT> tag is set to submit or reset rather than button. For example, the submit and reset buttons in the earlier screenshot were created using:

<INPUT TYPE="submit" VALUE="Submit" NAME="submit1">
<INPUT TYPE="reset" VALUE="Reset" NAME="reset1">

These buttons have special purposes, which are not related to script.

When the submit button is clicked, the form data from the form that the button is inside gets automatically sent to the server, without the need for any script.

When the reset button is clicked, all the elements in a form are cleared and returned to their default values; the values they had when the page was first loaded.

The submit and reset buttons have corresponding objects called Submit and Reset, which have exactly the same properties, methods, and events as a standard Button object.




5 RELATED COURSES AVAILABLE
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
INTRODUCTION TO JAVA PROGRAMMING
The aims of this Java training courses is to understand the role that Java plays on the Internet; describe the be....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
MICROSOFT VISUAL BASIC 5.0 CLIENT SERVER DEVELOPMENT
This course teaches the skills required to develop client server applications using MS Visual Basic 5.0 Enterpris....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Tuesday 22nd May 2012  © COPYRIGHT 2012 - website design by Website Design by Visualsoft