Textbox watermark label with Protoype Javascript library

First we'll outline the desired functionality:

  • The textbox should show the label inside the textarea, possibly in a different colour to when normal text is entered, to highlight that it is just a label.
  • When the textbox gains focus (clicked on / tabbed to) the label should disappear.
  • When the textbox loses focus the label should reappear if (and only if) no other text has been entered.
  • The solution should degrade graceful in the absence of JavaScript

As we want to write good semantic XHTML we'll start with the following code:

<label for="searchbox">Search</label>
<input type="textbox" name="q" id="searchbox" value="" />

So with JavaScript disabled we'll get a standard label outside the textbox. If we want the fall-back behaviour to be hide the label, we can do that with CSS.

Now we'll create our Prototype class:

var WatermarkLabel = Class.create({
	initialize: function(id) {
		this.textbox = $(id);
		this.text = '';
 
		var labels = $$('label[for="' + id + '"]');
		if(labels.length) {
			this.text = labels[0].innerHTML;
			Element.hide(labels[0]);
		}
 
		this.textbox.observe('focus', this.focus.bindAsEventListener(this));
		this.textbox.observe('blur', this.blur.bindAsEventListener(this));
		this.blur();
	},
 
	focus: function(event) {
		if(this.empty)
			this.textbox.value = '';
		this.empty = false;
		this.textbox.removeClassName('empty');
	},
 
	blur: function(event) {
		if(this.textbox.value == '') {
			this.textbox.value = this.text;
			this.textbox.addClassName('empty');
			this.empty = true;
		}
	}
});

To initalise the code we simply use:

new WatermarkLabel('searchbox'); //the textbox's "id"

The constructor (initialize) creates a handle on the textbox (this.textbox). It then finds the first associated label, stores it's value (this.text) and hides it. Finally it sets up the event observers on the textbox (blur and focus) and fires the blur event.

The focus and blur events determine if the textbox is empty and clear/restore the label, and add/remove the "empty" class, so the textbox can be styled differently when the label is being show (usually a lighter text colour for the watermark effect).

Further complications may arise if you have other scripts pointing at your textbox that could change it's value. For this we need to set up another listener, and trigger it whenever we change the textbox value (unfortunately we can't manually trigger the 'change' event). Prototype's custom events let us achieve this nicely.

Add the following observer after the first two:

	this.textbox.observe('value:changed', this.change.bindAsEventListener(this));

and add the following method to the class:

	change: function(event) {
		this.empty = (this.textbox.value == '');
		this.textbox.value == '' ? this.blur(event) : this.focus(event);
	}

then whenever we change the value of the textbox with another piece of code we just have to remember to fire the event using:

	textboxElement.fire('value:changed');

All scripts are Public Domain.

Comments

You Are Genius ..................

Do you have a working example/demo?

A variation of this is use on Cantorion.org.

Thanks! But, how do you keep the label from being submitted with a form. I'm using Ruby on Rails.

Keep a handle on all your WatermarkLabel objects (store them in an array if you have more than one):

var watermarks = [];
var w = new WatermarkLabel('input');
watermarks.push(w);

Then when you submit your form, simulate the focus event on each object:

form.onSubmit = function() {
watermarks.each(function(w) {
w.focus();
});
};

I found out something like this works:

function clearWaterMark()
{
var txt = document.getElementById(id_of_text_field);
if (txt.value == '[label]') txt.value = '';
}

Привет всем!
Нашел интересные фотоподборки на этом сайте: http://agentorange.ru :
http://agentorange.ru/art-foto-interesnoe/5798-ogorod-v-gorode.html Огород в городе
http://agentorange.ru/art-foto-interesnoe/3063-krayne-opasnye-produkty-d... Крайне опасные продукты для беременных женщин
http://agentorange.ru/art-foto-interesnoe/2142-novaya-kollekciya-massimo...

Привет всем!
Нашел интересные фотоподборки на этом сайте: http://agentorange.ru :
http://agentorange.ru/art-foto-interesnoe/5798-ogorod-v-gorode.html Огород в городе
http://agentorange.ru/art-foto-interesnoe/3063-krayne-opasnye-produkty-d... Крайне опасные продукты для беременных женщин
http://agentorange.ru/art-foto-interesnoe/2142-novaya-kollekciya-massimo...

Привет всем участникам!
Нашел прикольные новости на этом сайте: http://hellbro.ru :
http://hellbro.ru/interesnoe/10057-so-dna-barenceva-morya-podnyali-ameri... Со дна Баренцева моря подняли американские танки
Карлики и гиганты в мире животных
http://hellbro.ru/foto-prikoly-interesnoe/7673-hellouin-blizko.html

Производствосветодиодных электронных табло, бегущих строк, табло для АЗС

gsn casino games online casino games play casino | mgm online casino

gsn casino games online casino games play casino | mgm online casino

online casinos for us players casino slots online casino online casino gambling - https://onlinecasinoplay.us.org/ - online casino bonus - https://onlinecasinoplay.us.org/ - gsn casino games

online gambling casino play mgm online casino casino real money - https://onlinecasinoplay.us.org/ - gsn casino slots - https://onlinecasinoplay.us.org/ - casino bonus

casino play casino online online casino bonus online casino - https://onlinecasinoplay.us.org/ - casino online - https://onlinecasinoplay.us.org/ - casino play

slots for real money free online casino slots online casino bonus online casino slots - https://onlinecasinoplay.us.org/ - casino play - https://onlinecasinoplay.us.org/ - online casino real money

online casino games slots for real money bovada casino best online casino - https://onlinecasinoplay.us.org/ - online casino slots - https://onlinecasinoplay.us.org/ - online casinos for us players

online casino games slots for real money bovada casino best online casino - https://onlinecasinoplay.us.org/ - online casino slots - https://onlinecasinoplay.us.org/ - online casinos for us players

real money casino casino blackjack online casino gambling casino blackjack - https://onlinecasinoplay.us.org/ - casino real money - https://onlinecasinoplay.us.org/ - online casino

real money casino casino blackjack online casino gambling casino blackjack - https://onlinecasinoplay.us.org/ - casino real money - https://onlinecasinoplay.us.org/ - online casino

gsn casino slots gsn casino online casino slots mgm online casino | https://online-casinos.us.org/ - online gambling https://online-casinos.us.org/ - online casino bonus

gsn casino slots gsn casino online casino slots mgm online casino | https://online-casinos.us.org/ - online gambling https://online-casinos.us.org/ - online casino bonus

best online casinos casino games casino blackjack real money casino | https://onlinecasinoplay777.us.org/ - casino bonus https://onlinecasinoplay777.us.org/ - casino slots

casino real money online casino games casino slots casino online - https://onlinecasinoplay.us.org/ - online gambling casino - https://onlinecasinoplay.us.org/ - gsn casino games

online casino real money online casinos online casino bonus betfair online casino | https://online-casinos.us.org/ - casino slots https://online-casinos.us.org/ - gsn casino games

online casino slots free online casino slots bovada casino casino real money | https://onlinecasinoplay777.us.org/ - bovada casino https://onlinecasinoplay777.us.org/ - real money casino

online casino real money online gambling casino free online casino slots online casino | https://online-casinos.us.org/ - online casino bonus https://online-casinos.us.org/ - play casino

online casino real money online gambling casino free online casino slots online casino | https://online-casinos.us.org/ - online casino bonus https://online-casinos.us.org/ - play casino

gsn casino slots online casino gsn casino games casino games - https://onlinecasinoplay.us.org/ - casino online - https://onlinecasinoplay.us.org/ - gsn casino slots

mgm online casino gsn casino casino bonus casino online - https://onlinecasinoplay.us.org/ - mgm online casino - https://onlinecasinoplay.us.org/ - online casinos for us players

casino real money play casino online casino gambling betfair online casino | https://online-casinos.us.org/ - best online casinos https://online-casinos.us.org/ - play casino

casino real money play casino online casino gambling betfair online casino | https://online-casinos.us.org/ - best online casinos https://online-casinos.us.org/ - play casino

online casino bonus gsn casino games real money casino casino slots | https://onlinecasinoplay777.us.org/ - online gambling https://onlinecasinoplay777.us.org/ - best online casinos

online gambling casino
online casino bonus
gsn casino games
https://onlinecasinoplay.us.org/ - online casinos for us players
https://onlinecasinoplay.us.org/ - online gambling casino
https://onlinecasinoplay.us.org/ gsn casino slots

best online casinos casino real money real money casino gsn casino games | https://online-casinos.us.org/ - gsn casino games https://online-casinos.us.org/ - online casino slots

casino slots gsn casino slots online casino bonus casino real money - https://onlinecasinoplay.us.org/ - casino bonus - https://onlinecasinoplay.us.org/ - online casino games

zone online casino vegas world jack online casino casinos online vegas slots casino online | https://onlinecasinodd.com/ - casino slot https://onlinecasinodd.com/ - doubleu casino

online casinos world class casino slots masque twin river online casino online casino gambling | https://onlinecasino888.us.org/ - pechanga casino https://onlinecasino888.us.org/ - doubledown casino promo codes

casino play online casino mgm online casino casino bonus - https://onlinecasinoplay.us.org/ - slots for real money - https://onlinecasinoplay.us.org/ - casino play

casinos in iowa gsn casino on facebook royal river casino posh casino online | https://onlinecasinodd.com/ - chumba casino https://onlinecasinodd.com/ - play casino

zone online casino log in winstar world casino doubledown casino promo codes paradise casino | https://onlinecasino888.us.org/ - mgm online casino https://onlinecasino888.us.org/ - twin river online casino

zone online casino log in winstar world casino doubledown casino promo codes paradise casino | https://onlinecasino888.us.org/ - mgm online casino https://onlinecasino888.us.org/ - twin river online casino

online casino games free online casinos gsn casino games hallmark casino online | https://onlinecasinodd.com/ - online casino games free https://onlinecasinodd.com/ - casino online

Pages

Add new comment