Semantic in-field input label with jQuery

Homepage > Diary > Coding > Semantic in-field input label with jQuery

Wed 12/24/2008

in Coding, JavaScript

Recent moods on input fields presentation tend to insert as initial value of an empty form field its label value.

An example of this can be found on the Facebook Homepage.

Facebook Input Label

Facebook Input Label

Though this is a space saving way to show labels and a much more focused way to point out the expected field value, addressing it by the value attribute introduces a semantic issue, since that’s not at all an initial value, not counting that pulling apart the label tag is an accessibility fault.

To overcome this problem we can rely on the powerfull JavaScript framework jQuery, to build an unobtrusive plugin mantaining both accessibility and usability. Let’s see:

1) Link the script in the head section:

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.inputLabel.js" type="text/javascript"></script>

2) Semantically correct XHTML:

<label for="myInput">Email here<label><nput type="text" name="myInput" id="myInput" />

3) Run the plugin on document load (place this in a script tag before the end of head):

$(function () {
      $("#myInput").inputLabel();
});

That’s all. You can pass an options’ object to change, for example, the inserted value, or to prevent the plugin for running on already filled or pre-filled fields:

  • color: initial text color (default : “#666″)
  • e: event which triggers label text clearing (default: “focus”)
  • force: execute this script even if input value is not empty (default: false)
  • keep: if value of field is empty on blur, re-apply label text (default: true)

Check the demo page to see the script in action or download the source code below:
jQuery inputLabel Plugin (2.18 KB)

Share this:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • blogmarks
  • DZone
  • LinkedIn
  • MySpace
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Ping.fm
  • Yahoo! Buzz
No Related Posts

Tags: , ,

5 Responses to this post:

  • Patrick says:

    Work well but need some modifications for password input…it just show dots!

    • Dwight Jack says:

      Thanx, i know the password input issue.
      Im planning a second version with some refactoring and new features, including this.
      Since now i can say that switching from password to text input type isn’t possible on IE, but have to check it deeper.

  • Colin says:

    Hey I used the and really liked it. I wanted to be able to use it on whole forms and not individual text inputs so I changed innerText to be an array that is indexed by the id of each text field. That way I can call it like

    $('#myForm input.text').inputLabel();

    • Dwight Jack says:

      Ehy yeah cool hint :)
      Since jQuery 1.4 has been released lately and the rise of the new placeholder attribute in HTML5, i think i’ll be able to release a new version adding your hint too.

      Thanx for ur feedback!

  • Colin says:

    I’ve updated the code to support multiple inputs. It also supports password fields by cloning the object.

    You can get the code here: http://breezyfaq.com/js/jquery/inputlabel.js

Leave a Reply ()

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

* required.