DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Arvind has posted 2 posts at DZone. View Full User Profile

Toggling Default Value of Textbox on Focus

11.28.2012
| 4969 views |
  • submit to reddit
var t = document.getElementById('ID');
t.onfocus = hideText;
t.onblur = showText;

function hideText() {
	if(this.value == this.defaultValue) {
		this.value = '';
	}
}

function showText() {
	if(this.value == '') {
		this.value = this.defaultValue;
	}
}

You might have noticed the textboxes on most of good looking sites that have some default value written in them. When you click on the textbox, it becomes blank but as the focus loses, the default value again appends to the text box. Well this is a simple trick with JavaScript. Lets see the code for performing this trick.