Treehouse

Create Classy Inputs Using MooTools’ OverText

By on  

The MooTools More library is a goldmine. A treasure chest. Pirates booty, if you will (and, of course, I will). More is full of plugins that add a lot of class and functionality to your website with minimal effort. OverText is one of those plugins. OverText generates labels on top of your input boxes that serve as directives to your users as to what they should place into a given field.

The HTML

<input type="text" name="first_name" id="first-name" title="Your first name..." />

Nothing special here -- just an input box with a title attribute containing the desired label text.

The CSS

.overTxtLabel { color:#999; }

I've chosen to style my OverText labels with a lighter color -- feel free to customize however you'd like.

The MooTools JavaScript

window.addEvent('load',function() {
	new OverText('first-name',{ //there are many more options available!!
		positionOptions: {
			offset: {
				x: 8,
				y: 8
			}
		}
	});
});

The usage of OverText doesn't even require a description. You feed it a text field, any custom options you'd like, and that's it.

I love how MooTools can give your website an instant upgrade with plugins like this. MooTools FTW!

ydkjs-2.png

Recent Features

Incredible Demos

  • MooTools, mediaboxAdvanced, and&nbsp;Mexico

    The lightbox is probably one of my favorite parts of the Web 2.0 revolution. No more having to open new windows (which can bog down your computer quite a bit) to see a larger image, video, etc. Instead, the item loads right into the...

  • Styling CSS Print Page&nbsp;Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...

Discussion

  1. Pavel

    Can you do this for jQuery?

  2. I think the main argument against these nice label / input combo’s are that once the form is completed, if the user wants to go back and check something, they can’t quickly find the field they’re looking for…especially with non-standard info.

  3. Adriaan, I wouldn’t use this on its own. I’d still have some text near the input. For instance, I would have some text next to the input that read “Phone Number” and the OverText would be the hint text “415-555-5555″

  4. I still prefer:

    <input type=”text” id=’textbox’ title=”Enter First Name” onblur=”if(this.value==”){this.value=this.title;}”
    onfocus=”if(this.value==this.title)this.value=”” />

  5. V1

    I would suggest adding a check for support of the placeholder attribute. And than provide wrapper for browser who do not support this.

    This ways you can separate your content from your functionality and leverage HTML5 technologies where possible.

  6. shockiii

    Would be better if the label tag was already there. So instead of creating the label element using the input title, you’d just look for it by the “for” attribute and use it. With your version, no javascript, no label. Plus, it’s easier to read the html in this way.

  7. David this is a great post with yet another simple amazing MooTools plugin. I don’t use MooTools on my site but jQ. I would love to see what you could write up for jQ. Thanks.

  8. Adam

    But what with the Form.Validator.Inline? The OverText stays in the same place after the form is validated, thus resulting in visual bug.

    lookie here: http://www.mooforum.net/help12/overtext-and-form-validator-inline-alts-aren-moving-t3508.html

  9. The solution there isn’t javascript oriented really but css. All you need to do is wrap your inputs in a container that has position:relative and the text will move with them.

  10. Daniel Steigerwald

    Just for fun, Google Closure implementation https://gist.github.com/756500

    “>http://jsfiddle.net/grgX8/ source code https://gist.github.com/756500

    https://gist.github.com/756500

    /embedded/”>

    Original Size: 614.97KB (126.03KB gzipped)
    Compiled Size: 18.59KB (7.19KB gzipped)

    Compilation FTW ;)

  11. Hi david, i found a bug when for example – images load, or when you resize the window,
    when these happends, the text is not on the right place.

    I found a fix – using the update() function and repeat it every second.

    OverText.update().periodical(1000);
    

    i think you should manchne it.

    sorry for my english

    • Just thought i should manchine it,
      The problem is when making a website from rtl.
      thats when you should use the code i put on the comment up.

      new OverText(el,{ //there are many more options available!!
      positionOptions: {
      position: 'upperRight',
      edge: 'upperRight',
      offset: {
      x: -7,
      y: 5
      }
      }
      });

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!