Over a million developers have joined DZone.

Toggling Default Value of Textbox on Focus

DZone's Guide to

Toggling Default Value of Textbox on Focus

· ·
Free Resource

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;

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}