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

Recent Features

  • An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • Spyjax:  Ajax For Evil Using Dojo

    The idea of Spyjax is nothing new. In pasts posts I've covered how you can spy on your user's history with both MooTools and jQuery. Today we'll cover how to check user history using the Dojo Toolkit. The HTML For the sake of this...

  • New MooTools Plugin:  ElementFilter

    My new MooTools plugin, ElementFilter, provides a great way for you to allow users to search through the text of any mix of elements. Simply provide a text input box and ElementFilter does the rest of the work. The XHTML I've used a list for this example...

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!