14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 ■ <strong>JavaScript</strong> <strong>and</strong> User Interaction: Navigation <strong>and</strong> Forms<br />

Each element inside the collection has several properties; which ones are supported is dependent on the type<br />

of the element. I’ll now list all the properties <strong>and</strong> list the elements that support this property in parentheses. We’ll go<br />

through the different elements in detail later on in this chapter:<br />

• checked: Boolean indicating whether or not the element was selected (buttons, check boxes,<br />

radio buttons)<br />

• defaultChecked: Boolean indicating whether or not the element was initially selected<br />

(check boxes, radio buttons)<br />

• value: The value of the element as defined in the value attribute (all elements except<br />

select boxes)<br />

• defaultValue: The initial value of the element (text boxes, text areas)<br />

• form: The form the element resides in (read only—all elements)<br />

• name: Name of the element (all elements)<br />

• type: The type of the element (read only—all elements)<br />

One special element type is the select box, which comes <strong>with</strong> a collection of its own, a property called<br />

options—but more on this later. Each element has a range of methods that is also dependent on the type of element.<br />

None of these methods expect any parameters.<br />

• blur(): Takes the focus of the user agent away from the element (all elements)<br />

• focus(): Puts the focus of the user agent on the element (all elements)<br />

• click(): Simulates the user clicking the element (buttons, check boxes, file upload fields,<br />

Reset <strong>and</strong> Submit buttons)<br />

• select(): Selects <strong>and</strong> highlights the text content of the element (password fields, text fields,<br />

<strong>and</strong> text areas)<br />

■ ■Note Notice that click() seems at first a bit odd, but it can be really helpful if you work on web applications,<br />

<strong>and</strong> the middle tier of your development environment does things to the submitting process of forms like Java Spring<br />

<strong>and</strong> .NET do. This is not a JS beginner’s environment, though, so it falls outside the scope of this book.<br />

HTML Attributes Not Contained in the Elements Collection<br />

In addition to using the properties of the elements collection, you can read <strong>and</strong> set (browser settings permitting,<br />

of course) the attributes of the element in question once you reach it via the forms <strong>and</strong> elements collections.<br />

For example, you could change the size of a text field by changing its cols <strong>and</strong> rows attributes:<br />

var myTextBox = document.forms[0].elements[2];<br />

if (myTextBox.type == 'textarea'){<br />

myTextBox.rows = 10;<br />

myTextBox.cols = 30;<br />

}<br />

228<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!