Java Reference
In-Depth Information
resulting number by 60 makes it minutes, by 60 again makes it hours, and fi nally you divide by 24 to
convert to your fi nal fi gure of difference in days. The Math object's abs() method makes negative num-
bers positive. The user may have set the fi rst date to a later date than the second, and since you want to
fi nd only the difference between the two, not which is earlier, you make any negative results positive.
The Math.floor() method removes the fractional part of any result and returns just the integer part
rounded down to the nearest whole number.
Finally, you write the difference in days to the txtDays text box in the page.
myForm.txtDays.value = daysDiff;
Summary
In this chapter, you looked at how to add a user interface onto your JavaScript so that you can interact with
your users and acquire information from them. Let's look at some of the things we discussed in this chapter.
The HTML form is where you place elements making up the interface in a page.
Each HTML form groups together a set of HTML elements. When a form is submitted to a
server for processing, all the data in that form are sent to the server. You can have multiple
forms on a page, but only the information in one form can be sent to the server.
A form is created with the opening tag
<form> and ends with the close tag </form> . All the
elements you want included in that form are placed in between the open and close <form> tags.
The <form/> element has various attributes — for client-side scripting, the name attribute is the
important one. You can access forms with either their name attribute or their ID attribute.
Each
<form> element creates a Form object, which is contained within the document object. To
access a form named myForm, you write document.myForm. The document object also has a
forms property, which is a collection containing every form inside the document. The fi rst form
in the page is document.forms[0], the second is document.forms[1], and so on. The length
property of the forms property (document.forms.length) tells you how many forms are on
the page.
Having discussed forms, we then went on to look at the different types of HTML elements that
can be placed inside forms, how to create them, and how they are used in JavaScript.
The objects associated with the form elements have a number of properties, methods, and
events that are common to them all. They all have the name property, which you can use to
reference them in your JavaScript. They also all have the form property, which provides a ref-
erence to the Form object in which that element is contained. The type property returns a text
string telling you what type of element this is; types include text, button, and radio.
You also saw that the methods
focus() and blur() , and the events focus and blur , are avail-
able to every form element object. Such an element is said to receive the focus when it becomes
the active element in the form, either because the user has selected that element or because you
used the focus() method. However an element got the focus, its focus event will fi re. When
another element is set as the currently active element, the previous element is said to lose its
focus, or to blur. Again, loss of focus can be the result of the user selecting another element or
the use of the blur() method; either way, when it happens the blur event fi res. You saw that
the fi ring of focus and blur can, if used carefully, be a good place to check things like the
validity of data entered by a user into an element.
Search WWH ::




Custom Search