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

slots for real money gsn casino games slots for real money online casino | https://onlinecasinodd.com/ - free online casino slots https://onlinecasinodd.com/ - casino bonus

casino games slots free hollywood casino online slots slot machine games free mohegan sun free online slots | https://casinoslotstime.com/ - vegas world free games online slots https://casinoslotstime.com/ - online slots real money

e https://cialiscanadaph24.com# canadian pharmacy cialis
canadian pharmacy cialis
cialis overdose
canadian pharmacy cialis cialis online canada

e https://cialiscanadaph24.com# canadian pharmacy cialis
canadian pharmacy cialis
cialis overdose
canadian pharmacy cialis cialis online canada

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

slotomania on facebook free games online slots gold fish casino slots free slots no download no registration | https://slotsonliners.com/ - free casino slots https://slotsonliners.com/ - slot machine games free

slotomania on facebook free games online slots gold fish casino slots free slots no download no registration | https://slotsonliners.com/ - free casino slots https://slotsonliners.com/ - slot machine games free

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

gsn casino gsn casino online casino gambling best online casino | https://onlinecasinodd.com/ - best online casino https://onlinecasinodd.com/ - online casino gambling

hot shot casino slots free vegas slots online play slots online free slot games online | https://casinoslotstime.com/ - free online slots no download https://casinoslotstime.com/ - online slots real money

slots for real money casino games online casinos for us players casino online | https://onlinecasinodd.com/ - best online casinos https://onlinecasinodd.com/ - online casino games

online casino slots play slots slots online free free slots online no download | https://slotsonliners.com/ - free slot play https://slotsonliners.com/ - hollywood casino online slots free

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

gsn casino slots casino real money casino play gsn casino slots | https://onlinecasino888.us.org/ - play casino https://onlinecasino888.us.org/ - online gambling

gsn casino slots casino real money casino play gsn casino slots | https://onlinecasino888.us.org/ - play casino https://onlinecasino888.us.org/ - online gambling

free casino slot games free casino slots games hollywood casino online slots free free slots online no download | https://casinoslotstime.com/ - slotomania on facebook https://casinoslotstime.com/ - slot games free

k https://viagrawithoutadoctorprescriptionph24.com# viagra without prescription
viagra without a doctor prescription usa
generic viagra without prescription
viagra side effects
homemade viagra news

k https://viagrawithoutadoctorprescriptionph24.com# viagra without prescription
viagra without a doctor prescription usa
generic viagra without prescription
viagra side effects
homemade viagra news

online gambling casino online gambling online casinos for us players online casino bonus | https://onlinecasinodd.com/ - casino bonus https://onlinecasinodd.com/ - online casino bonus

online gambling casino online gambling online casinos for us players online casino bonus | https://onlinecasinodd.com/ - casino bonus https://onlinecasinodd.com/ - online casino bonus

cashman casino slots free casino slots games free slots hollywood gold fish casino slots | https://slotsonliners.com/ - slot games free https://slotsonliners.com/ - online casino slots no download

casino online online gambling casino gsn casino casino bonus | https://onlinecasino888.us.org/ - bovada casino https://onlinecasino888.us.org/ - casino blackjack

slots for free gsn casino slots slotomania on facebook free slot play | https://casinoslotstime.com/ - slotomania on facebook https://casinoslotstime.com/ - casino games slots free

slots for free gsn casino slots slotomania on facebook free slot play | https://casinoslotstime.com/ - slotomania on facebook https://casinoslotstime.com/ - casino games slots free

online casino free online casino slots casino bonus casino blackjack | https://onlinecasinodd.com/ - casino online https://onlinecasinodd.com/ - mgm online casino

gold fish casino slots caesars free slots online online casino slots free online slots no download no registration | https://slotsonliners.com/ - free slots online https://slotsonliners.com/ - free slots games

casino blackjack online casino bovada casino casino online | https://onlinecasino888.us.org/ - online gambling casino https://onlinecasino888.us.org/ - casino blackjack

online casino bonus best online casino free online casino slots mgm online casino - https://onlinecasinoplay.us.org/ - casino online - https://onlinecasinoplay.us.org/ - free online casino slots

free slots no download free online casino slots world class casino slots vegas slots online | https://casinoslotstime.com/ - free penny slots online https://casinoslotstime.com/ - vegas slots free

free slots no download free online casino slots world class casino slots vegas slots online | https://casinoslotstime.com/ - free penny slots online https://casinoslotstime.com/ - vegas slots free

n https://viagrawithoutadoctorprescriptionph24.com# viagra without a doctors prescription
buy viagra without prescription
viagra without a doctors prescription
viagra without a doctors prescription
buy viagra without prescription viagra without a doctors prescription

n https://viagrawithoutadoctorprescriptionph24.com# viagra without a doctors prescription
buy viagra without prescription
viagra without a doctors prescription
viagra without a doctors prescription
buy viagra without prescription viagra without a doctors prescription

free video slots vegas world free slots free games for casino slots hollywood slot machines | https://slotsonliners.com/ - free penny slots https://slotsonliners.com/ - play slots

gsn casino mgm online casino casino online casino games | https://onlinecasinodd.com/ - online casino bonus https://onlinecasinodd.com/ - free online casino slots

gsn casino mgm online casino casino online casino games | https://onlinecasinodd.com/ - online casino bonus https://onlinecasinodd.com/ - free online casino slots

g https://cialiscanadaph24.com# cialis in canada
canadian pharmacy cialis
canadian pharmacy cialis 20mg
buy cialis canada cialis from canada

free casino slots games play slots free casino slot games gold fish casino slots | https://casinoslotstime.com/ - hollywood casino online slots free https://casinoslotstime.com/ - free slot games online

free casino slots games play slots free casino slot games gold fish casino slots | https://casinoslotstime.com/ - hollywood casino online slots free https://casinoslotstime.com/ - free slot games online

x viagra without a doctors prescription viagra no prescription
viagra without prescription viagra without prescription
https://viagrawithoutadoctorprescriptionph24.com# viagra without a doctor prescription

x viagra without a doctors prescription viagra no prescription
viagra without prescription viagra without prescription
https://viagrawithoutadoctorprescriptionph24.com# viagra without a doctor prescription

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

Pages

Add new comment