Providing Contextual Form Help with Mootools
I like many webdevelopers build a lot of forms. Forms are the bread and butter of web applications, and while making forms is getting easier for developers, users often still have a hard time with them. In these circumstances it is necessary to give them a help above and beyond ‘password’. Giving contextual feedback to users helps them understand what you are asking, and helps you the developer/designer communicate what you want the user to do. This reduces user frustration with obscure or difficult to understand labels. However, this all assumes you have useful help text. But lets not niggle about those details and get onto the how to part.
Adding help to the form helper
- $help = '<p class="help-text">Passwords should be 6-8 characters and must contain at least one non-letter.</p>';
This will create a form element, label and wrapping div that looks something like
- <div class="input password">
- <label for="UserPassword">Password</label>
- <input type="password" name="data[User][password]" value="" id="UserPassword" />
- <p class="help-text">Passwords should be 6-8 characters and must contain at least one non-letter.</p>
- </div>
Making it pretty
While this will work its not very pretty. What we need is some CSS and Javascript to spruce it up. We’ll keep the CSS pretty basic and simple.
- form div {
- clear:both;
- position:relative;
- padding:5px 0;
- }
- form div:after {
- content : '.';
- height:0;
- display:block;
- clear:both;
- visibility:hidden;
- padding:0;
- margin:0;
- }
- form input {
- width:150px;
- float:left;
- margin:0 0 0 8px;
- }
- form label {
- width:150px;
- text-align:right;
- display:block;
- float:left;
- }
- form div p.help-text {
- padding:8px;
- border:1px solid #CF9F6D;
- background: #FFFFB2;
- clear:both;
- -moz-border-radius: 8px;
- -webkit-border-radius: 8px;
- border-radius: 8px;
- }
- html.js form div p.help-text {
- position: absolute;
- top:-3px;
- left: 400px;
- visibility:hidden;
- margin:0;
- width:500px;
- }
That will get us some nice yellow help bubbles off to the side of our form elements. I didn’t design around IE, so if you use this CSS make sure you test it. The CSS without any javascript will still display the helper bubbles. This will allow the effect to gracefully degrade.
Making it fancy
Making it fancy is equally easy. I’ve tailored the above CSS to be compatible with what I wrote earlier about easily enabling progressive enhancement By using a simple class that grabs all the necessary elements and applies the proper events, we can even fancier Form elements. Its built with MooTools but should be easily portable to any other Javascript library.
- var FormHelper = new Class({
- Implements : Options,
- options : {
- 'selector' : 'input[type=text], input[type=radio], input[type=password], textarea, select',
- 'mode' : 'focus',
- 'helpSelector' : '.help-text'
- },
- elements : [],
- initialize : function(options) {
- this.setOptions(options);
- $$(this.options.selector).each(function(item){
- var parent = item.getParent();
- this.elements.push(parent);
- if (this.options.mode == 'enter') {
- parent.addEvent('mouseenter', this.show.bindWithEvent(parent, this.options.helpSelector));
- parent.addEvent('mouseleave', this.hide.bindWithEvent(parent, this.options.helpSelector));
- } else {
- item.addEvent('focus', this.show.bindWithEvent(parent, this.options.helpSelector));
- item.addEvent('blur', this.hide.bindWithEvent(parent, this.options.helpSelector));
- }
- }, this);
- },
- /**
- * Show an element
- *
- * Is used as a mouseEnter event.
- */
- show : function(e, helpSelector) {
- e = new Event(e);
- this.addClass('active');
- if (helpSelector) {
- var fieldHint = this.getChildren(helpSelector);
- if (fieldHint) {
- fieldHint.fade('in');
- }
- }
- },
- /**
- * Hide an element
- *
- * Is used as a mouseLeave event.
- */
- hide : function(e, helpSelector) {
- e = new Event(e);
- this.removeClass('active');
- if (helpSelector) {
- var fieldHint = this.getChildren(helpSelector);
- if (fieldHint) {
- fieldHint.fade('out');
- }
- }
- }
- });
Currently all the helpers are visible on page load, adding one bit of Javascript and one bit of CSS and some alterations to our previous code we can make a nice gracefully degrading effect. I’m a big fan of graceful degradation whenever possible. Keeping things accessible is good for everyone in the end. If you don’t already have a domready
event setup add one in now.
- window.addEvent('domready', function() {
- var myForm = new FormHelper();
- });
This will build the FormHelper and add all the necessary events. In addition to hiding / showing our help bubbles it adds an active class to the containing div, providing another CSS hook.
Example
You can find an example of this in action here
The javascript class will be available in my gitHub. I will be adding in features as I find the need to build them. Or you could fork it and add features it.
Mootools is great!
anonymous user on 9/26/08
getChildren does not work for me on mootools 1.2.1
lanG on 5/20/09
IanG: There is a demo page on the article, you can check the code there. Perhaps you forgot to pass a
this
in to specify scoping.mark story on 5/21/09