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

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

winstar casino best online casino vegas slots casino online hollywood online casino | https://onlinecasino888.us.org/ - gossip online casino https://onlinecasino888.us.org/ - slotomania slot machines

play slots free casino slots free slots online no download vegas free slots | free games online slots free slot games

free online casino slots free slot machine games hollywood casino free slots online free online slots no download no registration | gsn casino slots free slots games online

free online casino slots free slot machine games hollywood casino free slots online free online slots no download no registration | gsn casino slots free slots games online

casino real money bovada casino best online casinos online casino bonus - https://onlinecasinoplay.us.org/ - casino online - https://onlinecasinoplay.us.org/ - online gambling

casino real money bovada casino best online casinos online casino bonus - https://onlinecasinoplay.us.org/ - casino online - https://onlinecasinoplay.us.org/ - online gambling

free slots games online free online slots no download slot machines hollywood casino online slots free | https://online-casinoslots.us.org/ - hollywood casino free slot play https://online-casinoslots.us.org/ - turning stone online slots

free slots games online free online slots no download slot machines hollywood casino online slots free | https://online-casinoslots.us.org/ - hollywood casino free slot play https://online-casinoslots.us.org/ - turning stone online slots

hollywood casino free slots online free online slots vegas world free video slots free online slots vegas world | https://casinoslotsonline.us.org/ - slot games free https://casinoslotsonline.us.org/ - free slot games with no download

hollywood casino free slots online free online slots vegas world free video slots free online slots vegas world | https://casinoslotsonline.us.org/ - slot games free https://casinoslotsonline.us.org/ - free slot games with no download

slot machines free slots hollywood goldfish casino slots free free casino slots | https://casinoslotsonline.us.org/ - free online slots https://casinoslotsonline.us.org/ - free slots online no download no registration

slots for real money winstar world casino chinook winds casino gambling online | https://onlinecasinodd.com/ - foxwoods casino https://onlinecasinodd.com/ - online casino slots no download

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

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

casino slots free games play slots free slots no download no registration vegas free slots online | https://online-casinoslots.us.org/ - free video slots https://online-casinoslots.us.org/ - free online slots vegas world

royal river casino pala casino online nj winstar world casino firekeepers casino | https://onlinecasinodd.com/ - online casino gambling https://onlinecasinodd.com/ - casino games

royal river casino pala casino online nj winstar world casino firekeepers casino | https://onlinecasinodd.com/ - online casino gambling https://onlinecasinodd.com/ - casino games

cashman casino slots free video slots gold fish casino slots free slots no download | https://casinoslotsonline.us.org/ - free video slots https://casinoslotsonline.us.org/ - free games online slots

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

u https://viagragenericforsalerx.com# buy generic viagra
generic viagra
generic viagra online
buy generic viagra

free slots no download free vegas slots online free casino slots mohegan sun free online slots | https://online-casinoslots.us.org/ - da vinci diamonds free online slots https://online-casinoslots.us.org/ - gsn casino slots

casino slots dakota sioux casino online casino bonus winstar casino | https://onlinecasinodd.com/ - casinos online https://onlinecasinodd.com/ - real money casino

royal river casino casino real money casino games no download no registration mystic lake casino | https://onlinecasino888.us.org/ - bigfish casino online games https://onlinecasino888.us.org/ - casinos in iowa

royal river casino casino real money casino games no download no registration mystic lake casino | https://onlinecasino888.us.org/ - bigfish casino online games https://onlinecasino888.us.org/ - casinos in iowa

free online slots no download no registration free casino slots games free vegas slots online casino vegas free slots online | https://casinoslotsonline.us.org/ - free online slots no download https://casinoslotsonline.us.org/ - vegas free slots

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

mystic lake casino online casinos for us players twin river online casino parx casino online | https://onlinecasinodd.com/ - casino games no download no registration https://onlinecasinodd.com/ - casino games

free penny slots slot machines hollywood casino free slots online vegas free slots online | https://online-casinoslots.us.org/ - world class casino slots https://online-casinoslots.us.org/ - free slot games online

prairie meadows casino huge casino slots muckleshoot casino prairie meadows casino | https://onlinecasino888.us.org/ - vegas world casino slots https://onlinecasino888.us.org/ - doubledown casino facebook

prairie meadows casino huge casino slots muckleshoot casino prairie meadows casino | https://onlinecasino888.us.org/ - vegas world casino slots https://onlinecasino888.us.org/ - doubledown casino facebook

turning stone online slots free slots vegas turning stone online slots free online slots no download | https://casinoslotsonline.us.org/ - vegas casino slots https://casinoslotsonline.us.org/ - free slot games with no download

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

casino online online gambling casino foxwoods casino pala casino | https://onlinecasino888.us.org/ - casinos in iowa https://onlinecasino888.us.org/ - vegas world casino slots

free casino slots no download vegas casino slots vegas casino slots free online slots no download | https://online-casinoslots.us.org/ - free slot games with no download https://online-casinoslots.us.org/ - slots online free

mohegan sun free online slots free video slots free casino slot games online slot games | https://casinoslotsonline.us.org/ - vegas casino slots https://casinoslotsonline.us.org/ - free casino slots no download

empire casino online pechanga casino hallmark casino online online casino reviews | https://onlinecasinodd.com/ - online casino bonus https://onlinecasinodd.com/ - virgin casino online nj

empire casino online pechanga casino hallmark casino online online casino reviews | https://onlinecasinodd.com/ - online casino bonus https://onlinecasinodd.com/ - virgin casino online nj

casino online fortune bay casino cherokee casino stn play online casino | https://onlinecasino888.us.org/ - casino blackjack https://onlinecasino888.us.org/ - play casino

cashman casino slots cashman casino slots free play slots hot shot casino slots | https://online-casinoslots.us.org/ - free slot games https://online-casinoslots.us.org/ - hollywood casino free slot play

slots for free hollywood casino online slots free slots games slot machines | https://casinoslotsonline.us.org/ - free video slots https://casinoslotsonline.us.org/ - free slot games online

casino blackjack gsn casino games casino games play casino - https://onlinecasinoplay.us.org/ - online casinos - https://onlinecasinoplay.us.org/ - casino games

casino blackjack gsn casino games casino games play casino - https://onlinecasinoplay.us.org/ - online casinos - https://onlinecasinoplay.us.org/ - casino games

Pages

Add new comment