Over a million developers have joined DZone.

Toggling Default Value of Textbox on Focus

·

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.


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;
	}
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}