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

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

casino bonus online casino real money online casinos casino games - https://onlinecasinoplay.us.org/ - free online casino slots - https://onlinecasinoplay.us.org/ - best online casinos

casino bonus online casino real money online casinos casino games - https://onlinecasinoplay.us.org/ - free online casino slots - https://onlinecasinoplay.us.org/ - best online casinos

turning stone casino posh casino world class casino slots masque turning stone casino | https://onlinecasino888.us.org/ - real casino slots https://onlinecasino888.us.org/ - jack online casino

turning stone casino posh casino world class casino slots masque turning stone casino | https://onlinecasino888.us.org/ - real casino slots https://onlinecasino888.us.org/ - jack online casino

penny slots maryland live casino online virgin casino online nj choctaw casino durant oklahoma | https://onlinecasinodd.com/ - gsn casino on facebook https://onlinecasinodd.com/ - cashman casino

jackpot party casino slots on facebook online casino virgin casino online casino play | https://onlinecasino888.us.org/ - posh casino https://onlinecasino888.us.org/ - mgm online casino

jackpot party casino slots on facebook online casino virgin casino online casino play | https://onlinecasino888.us.org/ - posh casino https://onlinecasino888.us.org/ - mgm online casino

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

jack online casino casino slot best online casinos winstar world casino | https://onlinecasinodd.com/ - casinos online https://onlinecasinodd.com/ - hollywood casino play4fun

jack online casino casino slot best online casinos winstar world casino | https://onlinecasinodd.com/ - casinos online https://onlinecasinodd.com/ - hollywood casino play4fun

jackpot party casino gsn casino vegas casino online pechanga casino | https://onlinecasino888.us.org/ - hollywood online casino https://onlinecasino888.us.org/ - online casino real money

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

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

huge casino slots world class casino slots masque online casino games slotomania slot machines | https://onlinecasinodd.com/ - slotomania slot machines https://onlinecasinodd.com/ - online casino

huge casino slots world class casino slots masque online casino games slotomania slot machines | https://onlinecasinodd.com/ - slotomania slot machines https://onlinecasinodd.com/ - online casino

slot machines play slots online free slot play slotomania on facebook | online slot games gsn casino slots

virgin casino online nj hollywood casino online facebook huge casino slots ilani casino | https://onlinecasino888.us.org/ - online casino gambling https://onlinecasino888.us.org/ - harrah online casino

virgin casino online nj hollywood casino online facebook huge casino slots ilani casino | https://onlinecasino888.us.org/ - online casino gambling https://onlinecasino888.us.org/ - harrah online casino

slot machines free online slots games free slot machine games turning stone online slots | slot machines free slots no download no registration

slot machines free online slots games free slot machine games turning stone online slots | slot machines free slots no download no registration

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

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

huuuge casino rivers casino online casinos for us players big fish casino slots | https://onlinecasinodd.com/ - pala casino online https://onlinecasinodd.com/ - online gambling casino

best online casinos casino games online casinos for us players online casino games - https://onlinecasinoplay.us.org/ - online casinos for us players - https://onlinecasinoplay.us.org/ - gsn casino games

best online casinos casino games online casinos for us players online casino games - https://onlinecasinoplay.us.org/ - online casinos for us players - https://onlinecasinoplay.us.org/ - gsn casino games

casinos near me seneca niagara casino double down casino gsn casino | https://onlinecasino888.us.org/ - grand falls casino https://onlinecasino888.us.org/ - playmgm nj casino online

play free slots for fun free games for casino slots hollywood slots online free slot play no download | free slot machine games free casino slot games

free slot machine games hollywood casino free slot play free online slots online slots free | gsn casino slots free online slots vegas world

free slot machine games hollywood casino free slot play free online slots online slots free | gsn casino slots free online slots vegas world

m https://viagragenericforsalerx.com# viagra for sale
viagra without a doctor prescription
generic viagra
over the counter viagra

m https://viagragenericforsalerx.com# viagra for sale
viagra without a doctor prescription
generic viagra
over the counter viagra

caesars casino online mystic lake casino casino games no download no registration online casino slots no download | https://onlinecasinodd.com/ - sugarhouse casino online https://onlinecasinodd.com/ - virgin casino online nj

jackpot party casino high 5 casino mystic lake casino casino blackjack | https://onlinecasino888.us.org/ - casino slot https://onlinecasino888.us.org/ - prairie meadows casino

casino games slots free free casino slots games free slot games online casino slots no download | caesars free slots online slots games

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

free slots vegas world free vegas slots online casino play slots free slot machine games | gold fish casino slots vegas free slots online

free slots vegas world free vegas slots online casino play slots free slot machine games | gold fish casino slots vegas free slots online

hollywood casino online facebook winstar world casino real casino slots on facebook twin river online casino | https://onlinecasinodd.com/ - choctaw casino https://onlinecasinodd.com/ - online gambling

hollywood casino online facebook winstar world casino real casino slots on facebook twin river online casino | https://onlinecasinodd.com/ - choctaw casino https://onlinecasinodd.com/ - online gambling

online gambling sites betfair casino online nj slot machines online casino gambling | https://onlinecasino888.us.org/ - maryland live casino online https://onlinecasino888.us.org/ - best online casinos

online gambling sites betfair casino online nj slot machines online casino gambling | https://onlinecasino888.us.org/ - maryland live casino online https://onlinecasino888.us.org/ - best online casinos

free penny slots hollywood casino free slot play cashman casino slots hollywood casino free slot play | free slots hollywood free vegas slots online casino

vegas slots free slots for free free slots games free online slots games | free vegas slots online casino free casino slots

online gambling slots for real money online gambling play casino - https://onlinecasinoplay.us.org/ - best online casinos - https://onlinecasinoplay.us.org/ - play casino

pala casino online free online casino slots huuuge casino slots hollywood casino play4fun | https://onlinecasinodd.com/ - online casino no deposit bonus https://onlinecasinodd.com/ - sugarhouse casino

Pages

Add new comment