18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

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.

Chapter 11<br />

You may be wondering why you can’t just use a Submit button and disable it using<br />

onclick. The answer is that the button actually disables before the form is submitted,<br />

which then prevents the form from being submitted at all.<br />

Resetting forms<br />

If you want to provide the user a way to reset all form fields to their default values, you can use an<br />

HTML Reset button:<br />

<br />

Similar to a Submit button, a Reset button requires no scripting for the browser to know what to do<br />

when it is clicked. Also similar to the Submit button, a reset event fires when the button is clicked:<br />

<br />

Of course, you can also use the onreset event handler to cancel the form reset.<br />

The form does have a reset() method that can reset the form directly from script without using a Reset<br />

button:<br />

<br />

Unlike submit(), using reset() still fires the reset event and the onreset event handler is still<br />

executed.<br />

Resetting a form has fallen out of favor among <strong>Web</strong> developers because an increasing<br />

number of users have mistakenly reset the form instead of submitting it (often<br />

the Submit and Reset buttons are next to each other). If a form contains information<br />

in form fields when it is first loaded, a Reset button can be helpful because it resets<br />

the fields to their initial values. <strong>For</strong> forms that initially load without any information<br />

in form fields, it is recommended to avoid using a Reset button.<br />

Text boxes<br />

Two flavors of text boxes are used in HTML: a single-line version, , and a<br />

multiline version, .<br />

The element must have its type attribute set to “text” in order to display a text box. You<br />

then use the size attribute to specify how wide the text box should be in terms of visible characters (for<br />

instance, setting size to “10” means that only 10 characters are visible at one time). The value attribute<br />

specifies the initial value of the text box and the maxlength attribute specifies how many characters are<br />

342

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

Saved successfully!

Ooh no, something went wrong!