Inline Labels in Form Fields using Javascript

What?
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.

Example?
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:".

Why?
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.

How?
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:
copyraw
<input 
	type="text" 
	name="name" 
	style="color:grey" 
	value="Name:" 
	onfocus="
		if(this.value.split(' ').join('')=='Name:'){
			this.value='';
			this.style.color='black';
		}" 
	onblur="
		if(this.value.split(' ').join('')==''){
			this.value='Name:';
			this.style.color='grey';
		}else{
			this.style.color='black';
		}"
/>
  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.              this.style.color='black'
  10.          }" 
  11.      onblur=" 
  12.          if(this.value.split(' ').join('')==''){ 
  13.              this.value='Name:'
  14.              this.style.color='grey'
  15.          }else{ 
  16.              this.style.color='black'
  17.          }" 
  18.  /> 

Explanation
  • 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.

Additional
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.
copyraw
this.value.split(' ').join('')

-- split this value into an array separated by the delimiter character "<space>"
-- join this array back replacing any spaces with nothing

Examples:
-- If field is "Joes Just Joking!" yields "JoesJustJoking!"
-- If field is " Space is a big place " yields "Spaceisabigplace" 

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

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

surname   change   give   forename   case   normally   user   array   check   black   label   back   specific   solution   clear   enter   cursor   javascript   another   attribute   note   type   clicks   yields   name   onblur   color   onfocus   leave   moves   split   join   code   indicate   default   outside   giving   spaces   form   still   field   text   value   click   grey   page   typing   happens   using   empty   JoelLipman.Com

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.