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-4.png

Recent Features

  • How I Stopped WordPress Comment&nbsp;Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • CSS&nbsp;Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • HTML5 Context&nbsp;Menus

    One of the hidden gems within the HTML5 spec is context menus. The HTML5 context menu spec allows developers to create custom context menus for given blocks within simple menu and menuitem elements. The menu information lives right within the page so...

  • CSS Background&nbsp;Animations

    Background animations are an awesome touch when used correctly.  In the past, I used MooTools to animate a background position.  Luckily these days CSS animations are widely supported enough to rely on them to take over JavaScript-based animation tasks.  The following simple CSS snippet animates...

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!