Home » Articles » Using javascript

Using javascript

by sheetal vadhera
Forms are a very common way of interacting on the Internet and you can see them used everywhere - to collect comments, for registering products, even for navigation. The problem is that sometimes we don't use things in the way they were intended and when we 'stray' from the ideal state, problems crop up. We as webmasters can't always identify accessibility problems when we code, but people who use screen-readers or other assistive technologies can. Here are some guidelines for you to use to code accessible forms.

Screen-readers are really buggy in their implementation of javascript so leave it out or make sure it doesn't interfere with the normal operation of filling out and submitting a form (as I have done below). Definitely don't use scripted form controls to change the page. Forms weren't designed to be used as navigation anyway - there are better ways to build a nav.

Lets look at some important form elements that sometimes get forgotten but should always be included.

The legend tag can hold important information about your form and should always be used to describe the intention of your form.

If you have several distinct areas or groups of form fields fieldsets can come in very handy. I can think of several examples of information grouping in forms but consider this one - billing address, shipping address, credit card information. These three distinct groups of information can be organized and displayed using fieldsets. Each fieldset can have its own legend and the legends should be those three group descriptors.

The label tag is a very important little tag. The label tag wraps around input descriptors such as 'first name:' or 'last name:' and is associated to a form element in your markup using the 'for=' attribute. A label should always be in very close proximity to its associated input for clarity. Remember: you can style a label just like any other element using CSS.

Sometimes using a table to layout a form can cause an interruption in the natural flow of label / input pairs. Just be sure to remember that being overly creative with your form layout is not a very good idea. Try to stick to the usual way and keep those labels close to their form fields.

Taking a few moments to think about your coding goals before you start can prevent accessibility issues. Following the guidelines set out by the W3C for standards in coding will usually eliminate problems. Standards compliant code will generally be better optimized for search engines and screen-readers alike.

Part II

Let's build an accessible compact form using javascript

So now that you know a little bit about web accessibility and forms, we can put it into place by building an accessible compact form. What's a compact form you say? A compact form is a form that takes up less space. It looks like you have entered the name of the field (eg.first name:) as a value in the input, but really it is a label hovering there. When the user clicks to input their data (gives the form element 'focus'), the label is swooshed off the screen, and when they click away from the input (removes 'focus' and is called 'blur') the script checks to see if data has been entered or not, and replaces the label or leaves it off-screen depending on what it found.

Using Javascript for extra presentation is quite popular these days but here is the caution: always make sure that you can use your web page with javascript turned off. The firefox developer toolbar that I told you about last month is excellent for this kind of test.

I have thoroughly commented the javascript in the working example and hopefully that will help you to understand what I have done. The general idea? - I am using javascript to change the class name on the labels of the form. There are three classes:

regLabel - how it will display without javascript enabled

compactLabel - the label appears over the form field

compactLabelShift - having moved the label off to the side so it is no longer visible

There is another possible state to consider that covers the situation where both javascript and css are disabled. This view relies on pure markup and as you can see the form is still highly usable and easy to tab through.

About the Author

Did you find this article useful? For more useful tips and hints, points to ponder and keep in mind, techniques, and insights pertaining to credit card, do please browse for more information at our websites. http://www.yoursgoogleincome.com http://www.freeearningtip.com

Thursday, Nov 12th, 2009