25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

176<br />

CHAPTER 7 ■ USING THE <strong>HTML</strong>5 FORMS API<br />

Using the <strong>HTML</strong>5 Forms APIs<br />

Now that we’ve spent some time familiarizing ourselves with the new form element types, let’s turn to<br />

the attributes and APIs that are present on both the old and new form controls. Many of them are<br />

designed to reduce the amount of scripting needed to create a powerful web application user interface.<br />

You may find that the new attributes give you the power to enhance your user interface in ways that you<br />

had not considered. Or, at the very least, you may be able to remove blocks of script in your existing<br />

pages.<br />

New form attributes and functions<br />

First, we’ll consider new attributes, functions, and a few elements that did not previously exist in earlier<br />

versions of <strong>HTML</strong>. Like the new input types, it is generally safe to use these attributes today, whether or<br />

not your target browser supports them. This is because the attributes will be safely ignored by any<br />

browser on the market today if the browser does not understand them.<br />

The placeholder Attribute<br />

The placeholder attribute gives input controls an easy way to provide descriptive, alternate hint text<br />

which is shown only when the user has not yet entered any values. This is common in many modern<br />

user interface frameworks, and popular JavaScript frameworks have also provided emulation of this<br />

feature. However, modern browsers have it built-in.<br />

To use this attribute, simply add it to an input with a text representation. This includes the basic text<br />

type, as well as the semantic types such as email, number, url, etc.<br />

Runner: <br />

In a supporting browser, such as Google Chrome, this causes the field to display a faint version of<br />

the placeholder text which will disappear whenever the user or application puts focus into the field, or<br />

whenever there is a value present.<br />

The same attribute, when running in a non-supporting browser, will just be ignored, causing the<br />

default field behavior to display.<br />

Similarly, whenever a value is entered in the field, the placeholder text will not appear.

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

Saved successfully!

Ooh no, something went wrong!