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!

Recent Features

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Submit Button Enabling

    "Enabling" you ask? Yes. We all know how to disable the submit upon form submission and the reasons for doing so, but what about re-enabling the submit button after an allotted amount of time. After all, what if the user presses the "stop"...

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!