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

mohegan sun casino hollywood casino online jack online casino harrah online casino | https://onlinecasinodd.com/ - hollywood casino play4fun https://onlinecasinodd.com/ - seneca casino online games

casino games slots free online slots real money casino slots free games free vegas slots online casino | https://casinoslotsonline.us.org/ - free casino slots https://casinoslotsonline.us.org/ - free video slots

winstar casino twin river online casino prairie meadows casino empire casino online | https://onlinecasino888.us.org/ - gambling online https://onlinecasino888.us.org/ - online gambling

mgm online casino online casinos casino bonus gsn casino slots - https://onlinecasinoplay.us.org/ - casino blackjack - https://onlinecasinoplay.us.org/ - gsn casino games

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

slot machines slotomania on facebook free slots casino games jackpot party casino slots | https://casinoslotsonline.us.org/ - goldfish casino slots free https://casinoslotsonline.us.org/ - free penny slots

online gambling casino gsn casino gambling online mohegan sun casino | https://onlinecasino888.us.org/ - jackpot party casino facebook https://onlinecasino888.us.org/ - free online casino slots

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

usa online casino pala casino online harrah online casino plainridge casino | https://onlinecasinodd.com/ - vegas world casino slots https://onlinecasinodd.com/ - pechanga casino

free penny slots online slots online free hollywood casino online slots free free online slots | https://online-casinoslots.us.org/ - hollywood casino free slots https://online-casinoslots.us.org/ - hollywood casino online slots free

vegas slots casino online online casino bonus sugarhouse casino online online gambling sites | https://onlinecasino888.us.org/ - virgin casino online https://onlinecasino888.us.org/ - bovada casino

free video slots da vinci diamonds free online slots free slots no download no registration free vegas slots online casino | https://casinoslotsonline.us.org/ - free slot play no download https://casinoslotsonline.us.org/ - free casino slot games

jackpot party casino casino play penny slots online casino bonus | https://onlinecasinodd.com/ - san manuel casino https://onlinecasinodd.com/ - doubleu casino on facebook

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

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

gossip online casino pala casino online nj online casino slots online gambling casino | https://onlinecasino888.us.org/ - vegas slots casino online https://onlinecasino888.us.org/ - gambling sites

best online casinos gsn casino slots online casinos gsn casino slots - https://onlinecasinoplay.us.org/ - best online casino - https://onlinecasinoplay.us.org/ - free online casino slots

free slots online no download no registration free casino slot games free penny slots online online slots real money | https://casinoslotsonline.us.org/ - free slots no download no registration https://casinoslotsonline.us.org/ - free slots casino games

online gambling sites chinook winds casino pala casino online nj bonus casino | https://onlinecasinodd.com/ - four winds casino https://onlinecasinodd.com/ - sugarhouse casino

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

seneca niagara casino jackpot party casino facebook mystic lake casino gsn casino games | https://onlinecasino888.us.org/ - foxwoods resort casino https://onlinecasino888.us.org/ - vegas casino online

play slots caesars free slots online free games for casino slots hollywood free slots vegas world | https://slotsonliners.com/ - caesars free slots online https://slotsonliners.com/ - free slots online no download

online gambling online casino online gambling casino games | https://onlinecasinodd.com/ - casino slots https://onlinecasinodd.com/ - online gambling

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

slots online free free slot machine games best online slots vegas slots online free | https://casinoslotstime.com/ - world class casino slots https://casinoslotstime.com/ - cashman casino slots

online casino online casinos for us players mgm online casino casino slots | https://onlinecasino888.us.org/ - casino real money https://onlinecasino888.us.org/ - best online casino

n https://cialiscanadaph24.com# buy cialis from canada
canadian pharmacy cialis
buy cialis canada
canadian pharmacy cialis buy cialis from canada

n https://cialiscanadaph24.com# buy cialis from canada
canadian pharmacy cialis
buy cialis canada
canadian pharmacy cialis buy cialis from canada

mgm online casino gsn casino slots casino games online casino gambling | https://onlinecasinodd.com/ - best online casinos https://onlinecasinodd.com/ - online casinos for us players

mgm online casino gsn casino slots casino games online casino gambling | https://onlinecasinodd.com/ - best online casinos https://onlinecasinodd.com/ - online casinos for us players

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

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

hollywood casino free slots online casino games slots free free slot games online turning stone online slots | https://casinoslotstime.com/ - online slots free https://casinoslotstime.com/ - free online slots games

hollywood casino free slots online casino games slots free free slot games online turning stone online slots | https://casinoslotstime.com/ - online slots free https://casinoslotstime.com/ - free online slots games

free online casino slots mgm online casino online gambling casino best online casinos | https://onlinecasino888.us.org/ - online casinos https://onlinecasino888.us.org/ - slots for real money

free slots online no download hollywood casino online slots vegas slots online free games online slots | https://slotsonliners.com/ - mohegan sun free online slots https://slotsonliners.com/ - free games for casino slots hollywood

free slots online no download hollywood casino online slots vegas slots online free games online slots | https://slotsonliners.com/ - mohegan sun free online slots https://slotsonliners.com/ - free games for casino slots hollywood

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

online casino bonus best online casino best online casinos casino bonus | https://onlinecasinodd.com/ - online casino real money https://onlinecasinodd.com/ - free online casino slots

casino slots free games cashman casino slots free hollywood casino free slots online vegas free slots | https://casinoslotstime.com/ - free casino slot games https://casinoslotstime.com/ - free slots online

online casino gambling online casino online casino bonus casino online | https://onlinecasino888.us.org/ - online casinos for us players https://onlinecasino888.us.org/ - best online casinos

k https://sildenafilcitrateph24.com# sildenafil citrate
sildenafil dosage
sildenafil citrate 100mg
sildenafil
buy sildenafil online sildenafil

free online slots games vegas slots online free casino slots free free online slots no download no registration | https://slotsonliners.com/ - free slots vegas https://slotsonliners.com/ - slots for free

free online slots games vegas slots online free casino slots free free online slots no download no registration | https://slotsonliners.com/ - free slots vegas https://slotsonliners.com/ - slots for free

Pages

Add new comment