Sunday, March 29, 2015
Text Size

Inline Labels in Form Fields using Javascript

This is to describe how to change the value of a field of a form from it's default when it is clicked on so that it auto-clears and changes to how it normally works.

Note the value in the below field. Now click on it and it disappears, allowing you to type normally. Clear whatever you type so the field is empty and click somewhere on the page (not on a link as this will take you to another page) and the field returns to displaying "Name:".

I've noticed a lot of solutions now use JQuery or Mootools but most of these will not work in Internet Explorer 7 which is unfortunately still in use. I needed a back-to-basics solution and this is the one I have used since my fad of JavaScript 1.2 in the 90s. For broader compatibility, this is my recommended solution.

We're going to use the onBlur and onFocus attributes which do, as the name indicates, trigger events when the cursor is either in the field or not. The above field was created with the following code:

  1.  <input  
  2.       type="text"  
  3.       name="name"  
  4.       style="color:grey"  
  5.       value="Name:"  
  6.       onfocus=" 
  7.            if(this.value.split(' ').join('')=='Name:'){ 
  8.                 this.value=''; 
  9.       'black'
  10.            }"  
  11.       onblur=" 
  12.            if(this.value.split(' ').join('')==''){ 
  13.                 this.value='Name:'
  14.       'grey'
  15.            }else{ 
  16.       'black'
  17.            }" 
  18.  /> 

  • type="text" - standard attribute that tells the field this is an input field for single line of text.
  • name="name" - attribute that assigns a variable name to this field so we can reference it later.
  • style="color:grey" - add a styling to it by default (on initial load), in this specific case the color grey.
  • value="Name:" - give this field a default value. In this specific case, we are giving it the label value to indicate to the user to enter their Name (as in forename or surname).
  • onFocus() - What to do when the user moves their cursor into the field or clicks on it. In this specific case, if the value is "Name:", then clear the field and change the color to black (so when they type they can see what they're typing).
  • onBlur() - happens when the user clicks outside of the field or moves the cursor to another field. In this specific case, check if the field is empty, if it is, change the value back to "Name:" and the color back to grey. If it isn't, then leave as is and ensure the color is still black.

Note that instead of just saying if this value equals "Name:" I've added some code to trim the values. If the user types two spaces, then we want the field to understand this as "no one entered anything", treat it as empty.

  1.  this.value.split(' ').join('') 
  3.  -- split this value into an array separated by the delimiter character "<space>" 
  4.  -- join this array back replacing any spaces with nothing 
  6.  Examples: 
  7.  -- If field is "Joes Just Joking!" yields "JoesJustJoking!" 
  8.  -- If field is " Space is a big place " yields "Spaceisabigplace"  
  10.  Reminder: 
  11.  -- We're using this for a comparison condition and not trying to change the value. 
Add Comment





Help Develop!

Thank You! :c)

Connect: Google+

Visitors of the World

Latest Posts

  • Basic Android App using Google Maps and Current Location

    • Sat 28-Mar-15
      Hi Vsiqueira, I used to get a similar problem if I switched my android device off and on again.
      Joel L.  
    • Fri 27-Mar-15
      hi, im getting an erro, in the method MyLocation.getL ocation(), return null, how i can resolve ...
    • Wed 25-Mar-15
      Hi Rishikesh, I use a database on a webserver with all the latitudes and longitudes of the points of ...
      Joel L.  
    • Tue 24-Mar-15
      At the starting asa I run the app. I want to show list of garages available near by my current location.
  • JComments 2.3.0 with ReCaptcha in Joomla 2.5.x

    • Mon 23-Mar-15
      Worked like a charm first time! :lol: Getting bored of the spam comments. This should stop them. :P