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

play casino casino slots play casino play casino | https://onlinecasinoslotsmaxx.com/ - bovada casino https://onlinecasinoslotsmaxx.com/ - casino blackjack

vegas slots online hollywood casino online slots play slots real casino slots | https://casinoslotsplay.us.org/ - vegas casino slots https://casinoslotsplay.us.org/ - slots online

vegas slots online hollywood casino online slots play slots real casino slots | https://casinoslotsplay.us.org/ - vegas casino slots https://casinoslotsplay.us.org/ - slots online

slots games casino slots slot games gsn casino slots | https://casinoslotsonline.us.org/ - gsn casino slots https://casinoslotsonline.us.org/ - free online slots vegas world

slots games casino slots slot games gsn casino slots | https://casinoslotsonline.us.org/ - gsn casino slots https://casinoslotsonline.us.org/ - free online slots vegas world

casino bonus casino bonus online casino real money online casinos for us players | https://onlinecasinoslotsmaxx.com/ - best online casino https://onlinecasinoslotsmaxx.com/ - gsn casino slots

online casino bonus online gambling online casino games online gambling - https://onlinecasinoplay.us.org/ - online gambling - https://onlinecasinoplay.us.org/ - online casinos

online casino bonus online gambling online casino games online gambling - https://onlinecasinoplay.us.org/ - online gambling - https://onlinecasinoplay.us.org/ - online casinos

online casino bonus gsn casino games best online casinos casino games | https://onlinecasinoslotsmaxx.com/ - gsn casino slots https://onlinecasinoslotsmaxx.com/ - online casino slots

online slots real money casino slots slots online play slots online | https://online-casinoslots.us.org/ - real casino slots https://online-casinoslots.us.org/ - play slots

online slots real money casino slots slots online play slots online | https://online-casinoslots.us.org/ - real casino slots https://online-casinoslots.us.org/ - play slots

play slots free online slots no download real casino slots slot games | https://casinoslotsonline.us.org/ - turning stone online slots https://casinoslotsonline.us.org/ - best online slots

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

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

casino bonus bovada casino online casino slots online casino bonus | https://onlinecasinoslotsmaxx.com/ - online gambling https://onlinecasinoslotsmaxx.com/ - online casino games

casino bonus bovada casino online casino slots online casino bonus | https://onlinecasinoslotsmaxx.com/ - online gambling https://onlinecasinoslotsmaxx.com/ - online casino games

world class casino slots vegas slots online online slots real money real casino slots | https://online-casinoslots.us.org/ - gsn casino slots https://online-casinoslots.us.org/ - free online slots games

real casino slots gsn casino slots free online slots online slots | https://casinoslotsonline.us.org/ - play slots online https://casinoslotsonline.us.org/ - online slot games

online casino slots gsn casino online casino slots casino slots | https://onlinecasinoslotsmaxx.com/ - casino play https://onlinecasinoslotsmaxx.com/ - online casino games

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

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

online casino mgm online casino gsn casino online gambling - https://onlinecasinoplay.us.org/ - mgm online casino - https://onlinecasinoplay.us.org/ - bovada casino

online casino mgm online casino gsn casino online gambling - https://onlinecasinoplay.us.org/ - mgm online casino - https://onlinecasinoplay.us.org/ - bovada casino

online casinos betfair online casino mgm online casino casino real money | https://onlinecasinoslotsmaxx.com/ - best online casinos https://onlinecasinoslotsmaxx.com/ - online casino

t how does viagra work ed meds online without doctor prescription
starlite viagra viagra without a doctor prescription usa
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without doctor prescription

t how does viagra work ed meds online without doctor prescription
starlite viagra viagra without a doctor prescription usa
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without doctor prescription

r canadian pharmacy viagra ed meds online without doctor prescription
generic viagra without prescription levitra vs viagra
http://starlite.id/viagra-without-a-doctor-prescription/# generic viagra without prescription

eiyjmp cialis

jyrusp fda

jyrusp fda

cvbooc cialis

cvbooc cialis

etmpzg levitra

etmpzg levitra

k viagra without a doctors prescription viagra without a doctors prescription
starlite viagra viagra without doctor prescription
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without a doctor prescription usa

k viagra without a doctors prescription viagra without a doctors prescription
starlite viagra viagra without doctor prescription
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without a doctor prescription usa

g ed meds online without doctor prescription levitra starlite
fda browse around these guys
http://starlite.id/levitra/# levitra without a doctor prescription

g ed meds online without doctor prescription levitra starlite
fda browse around these guys
http://starlite.id/levitra/# levitra without a doctor prescription

Pages

Add new comment