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

cialis purchase online without prescription tadalafil tablets 20 mg dosage cialisvv.com
cialis uk http://cialisvv.com/
tadalafil 20mg india http://cialisvv.com/
real cialis without a doctor's prescription
use of cialistaking half a 20mg cialisdanger achat cialis internetcialis reactionscialis soft generic link lipitor ubuy cialis soft online get prescriptioncialis zyprexaaldara cialis
http://stardustpokerroom.net/__media__/js/netsoltrademark.php?d=cialisvv...

cialis purchase online without prescription tadalafil tablets 20 mg dosage cialisvv.com
cialis uk http://cialisvv.com/
tadalafil 20mg india http://cialisvv.com/
real cialis without a doctor's prescription
use of cialistaking half a 20mg cialisdanger achat cialis internetcialis reactionscialis soft generic link lipitor ubuy cialis soft online get prescriptioncialis zyprexaaldara cialis
http://stardustpokerroom.net/__media__/js/netsoltrademark.php?d=cialisvv...

cialis without a doctor's prescription in usa cialis generico en farmacias similares cialissy.com
cialis tablets australia http://cialissy.com/
cialis anda litigation http://cialissy.com/
cialis without a doctors prescription
research prevention white finger cialis researchcialis lowest pricecialis pfizerbuy cialis in beijingcialis cialis genuinerx net viagra viagracialis wavcialis kaufen in indiencialis new zealand samplecialis once a day bijwerkingen
http://sudoku8.com/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without a doctor's prescription in usa cialis generico en farmacias similares cialissy.com
cialis tablets australia http://cialissy.com/
cialis anda litigation http://cialissy.com/
cialis without a doctors prescription
research prevention white finger cialis researchcialis lowest pricecialis pfizerbuy cialis in beijingcialis cialis genuinerx net viagra viagracialis wavcialis kaufen in indiencialis new zealand samplecialis once a day bijwerkingen
http://sudoku8.com/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without prescriptions 20mg cialis lowest price cialisvv.com
cialis tablets australia http://cialisvv.com/
cialis 5mg tablets http://cialisvv.com/
cialis without a doctor's prescription
rx cialiscialis dosage informationindia cialis 100mg costbuy generic cialis online in canadavendita cialiscialis no perscriptioncialis im internet kaufenoffshore drugs cheap cialis
http://pennotw.com/__media__/js/netsoltrademark.php?d=cialisvv.com

buy prescription drugs without doctor cialis 5mg price comparison cialisvv.com
tadalafil 20mg tablets http://cialisvv.com/
venta cialis generico http://cialisvv.com/
cialis without a doctor's prescription from canada
generac cialis softcialis discounuse of cialisverschil cialis kamagracialis en bijwerkingenwhats stronger cialis viagracialis rezeptfrei fur die frau
http://shadaitrucking.com/__media__/js/netsoltrademark.php?d=cialisvv.com

tadalafil without a doctor's prescription cialis daily use cialisvi.com
cialis cena srbija http://cialisvi.com/
tadalafil generic india http://cialisvi.com/
order cialis without prescription
effet contraire du cialiscan i take viagra and cialis at same timefree cheap cialis pillscialis without a perscriptionbuy generic cialis online australia
http://alcaponescasino.net/__media__/js/netsoltrademark.php?d=cialisvi.com

tadalafil without a doctor's prescription cialis daily use cialisvi.com
cialis cena srbija http://cialisvi.com/
tadalafil generic india http://cialisvi.com/
order cialis without prescription
effet contraire du cialiscan i take viagra and cialis at same timefree cheap cialis pillscialis without a perscriptionbuy generic cialis online australia
http://alcaponescasino.net/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis without a doctors prescription buy tadalafil cialisonla.com
cialis tablets side effects http://cialisonla.com/
cialis 5mg coupon lilly http://cialisonla.com/
buy cialis without a doctor's prescription
cialis homepagephotographe vahin s sp cialiscialis soft shipped cod on saturday deliverycialis levitra staxyn and viagra pricesbuy cialis in canadacialis couponsque es mas potente el viagra o el cialiscialis bath tubscialis in australia
http://rubberandplasticsco.biz/__media__/js/netsoltrademark.php?d=cialis...

cialis without a doctors prescription buy tadalafil cialisonla.com
cialis tablets side effects http://cialisonla.com/
cialis 5mg coupon lilly http://cialisonla.com/
buy cialis without a doctor's prescription
cialis homepagephotographe vahin s sp cialiscialis soft shipped cod on saturday deliverycialis levitra staxyn and viagra pricesbuy cialis in canadacialis couponsque es mas potente el viagra o el cialiscialis bath tubscialis in australia
http://rubberandplasticsco.biz/__media__/js/netsoltrademark.php?d=cialis...

cialis canada prescription cialis tablets 20mg cialisvi.com
cialis 20 mg directions http://cialisvi.com/
cialis lowest price coupon http://cialisvi.com/
order cialis without prescription
cialis resultcialis dagelijkse dosiscialis v viagra comparisonbuy herbal cialis softcialis superior in diabetes
http://colorceutical.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis canada prescription cialis tablets 20mg cialisvi.com
cialis 20 mg directions http://cialisvi.com/
cialis lowest price coupon http://cialisvi.com/
order cialis without prescription
cialis resultcialis dagelijkse dosiscialis v viagra comparisonbuy herbal cialis softcialis superior in diabetes
http://colorceutical.com/__media__/js/netsoltrademark.php?d=cialisvi.com

how to get cialis without doctor cialis 5mg price walgreens cialissy.com
cialis online canada http://cialissy.com/
buy cialis without a doctor's prescription http://cialissy.com/
cialis without a doctor's prescription
buy cialis without a perscriptioncialisbuy cialis 10mg ukbrand vs generic cialiskosten fur cialis in der apothekecialis and alcohol effectscialis for sale in europais cialis like viagra
http://sailingfaith.com/__media__/js/netsoltrademark.php?d=cialissy.com

tadalafil without a doctor's prescription generic cialis tadalafil fast cialisvv.com
tadalafil 20mg for sale http://cialisvv.com/
cialis prices 20mg http://cialisvv.com/
cialis without a doctor's prescription
cialis-generika-bestellen in deutschlandcialis manufacturerdifference between viagra and cialis dosagebuy cialis onli necialis 10 mgcialis for heart healthsp cialis es de v lo
http://firerwood.com/__media__/js/netsoltrademark.php?d=cialisvv.com

cialis online without prescription cialis from canada cialissi.com
cialis generic availability http://cialissi.com/
tadalafil 10 mg tablets cost http://cialissi.com/
real cialis without a doctor's prescription
cialis vs viagra dosage comparisonbijsluiter van cialison line cialis softseo talk com cheap xanax cialiscialis kopen in apotheekbuy n cialis softcialis daily test
http://upupprods.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis online without prescription cialis from canada cialissi.com
cialis generic availability http://cialissi.com/
tadalafil 10 mg tablets cost http://cialissi.com/
real cialis without a doctor's prescription
cialis vs viagra dosage comparisonbijsluiter van cialison line cialis softseo talk com cheap xanax cialiscialis kopen in apotheekbuy n cialis softcialis daily test
http://upupprods.com/__media__/js/netsoltrademark.php?d=cialissi.com

tadalafil without a doctor's prescription tadalafil 5mg preco cialissy.com
generic for cialis medication http://cialissy.com/
cialis prices http://cialissy.com/
tadalafil without a doctor's prescription
mens health herbal cialis reviewcomparateur de prix viagra cialisbuy cialis pharmacycialis retin-a alessetrial packs viagra cialiscialis inwerktijdbuy real viagra levitra cialis online edrugstore.mdcialis kopen in frankrijkcialis 10 mgcialis 20 mg controindicazioni
http://chevroletcompletecare.com/__media__/js/netsoltrademark.php?d=cial...

tadalafil without a doctor's prescription tadalafil 5mg preco cialissy.com
generic for cialis medication http://cialissy.com/
cialis prices http://cialissy.com/
tadalafil without a doctor's prescription
mens health herbal cialis reviewcomparateur de prix viagra cialisbuy cialis pharmacycialis retin-a alessetrial packs viagra cialiscialis inwerktijdbuy real viagra levitra cialis online edrugstore.mdcialis kopen in frankrijkcialis 10 mgcialis 20 mg controindicazioni
http://chevroletcompletecare.com/__media__/js/netsoltrademark.php?d=cial...

cheap cialis without a doctors prescription tadalafil 5mg preco cialissy.com
buy cialis online cheap http://cialissy.com/
tadalafil generic 20mg http://cialissy.com/
cialis without a doctor's prescription
cialis in u scialis da 5 mg prezzo in farmaciacialis schadelijkcialis costaking 2 cialisover dose on cialiscialis 20mg online bestellencialis ed reviews
http://s-t-o-n-e.net/__media__/js/netsoltrademark.php?d=cialissy.com

buy cialis without a doctor's cialis prices cialisvi.com
venta cialis generico http://cialisvi.com/
cialis 20 mg best price india http://cialisvi.com/
cialis without a prescription
cialis message boardscialis 10 mg precio argentinacialis spendere pocogeneric viagra compare generic cialis pillsfree cialis softtabs onlinewirkt cialis bei diabetes
http://verified-renter.com/__media__/js/netsoltrademark.php?d=cialisvi.com

how to get cialis without doctor cialis 5 mg side effects cialissi.com
cialis tadalafil 40mg http://cialissi.com/
buy cheap cialis in canada http://cialissi.com/
cialis without a doctor's prescription usa
cialis ed reviewsdangers of finasteride and cialiscialis for daily usebuying cialis soft on linecutting pill cialiscialis lawyer ohiocialis golf open westerncialis for dogs
http://gguidemobile.net/__media__/js/netsoltrademark.php?d=cialissi.com

how to get cialis without doctor cialis 5 mg side effects cialissi.com
cialis tadalafil 40mg http://cialissi.com/
buy cheap cialis in canada http://cialissi.com/
cialis without a doctor's prescription usa
cialis ed reviewsdangers of finasteride and cialiscialis for daily usebuying cialis soft on linecutting pill cialiscialis lawyer ohiocialis golf open westerncialis for dogs
http://gguidemobile.net/__media__/js/netsoltrademark.php?d=cialissi.com

cialis purchase online without prescription tadalafil generic 20mg cialissv.com
generic for cialis daily 5 mg available when http://cialissv.com/
tadalafil without a doctor's prescription http://cialissv.com/
cialis online without prescription
what is b cialis bcialis bestellen via internetbuy cialis online ukervaring generieke cialisenter site natural cialiscialis severe headachecheapest cialis without prescription
http://rukabsicecream.com/__media__/js/netsoltrademark.php?d=cialissv.com

cialis purchase online without prescription tadalafil generic 20mg cialissv.com
generic for cialis daily 5 mg available when http://cialissv.com/
tadalafil without a doctor's prescription http://cialissv.com/
cialis online without prescription
what is b cialis bcialis bestellen via internetbuy cialis online ukervaring generieke cialisenter site natural cialiscialis severe headachecheapest cialis without prescription
http://rukabsicecream.com/__media__/js/netsoltrademark.php?d=cialissv.com

canadian pharmacy no prescription cialis tadalafil 5mg cialisonla.com
buy cialis online cheap http://cialisonla.com/
cialis tablets http://cialisonla.com/
cialis without a doctor's prescription
compare viagra levitron cialiscialis alcohol effectsviagra et cialis en meme tempsviagra cialis espa olcialis v viagra forum
http://joemad22.com/__media__/js/netsoltrademark.php?d=cialisonla.com

canadian pharmacy no prescription cialis tadalafil 5mg cialisonla.com
buy cialis online cheap http://cialisonla.com/
cialis tablets http://cialisonla.com/
cialis without a doctor's prescription
compare viagra levitron cialiscialis alcohol effectsviagra et cialis en meme tempsviagra cialis espa olcialis v viagra forum
http://joemad22.com/__media__/js/netsoltrademark.php?d=cialisonla.com

cialis without a doctors prescription generic cialis lowest price cialisonla.com
tadalafil without a doctor's prescription [url=http://cialisonla.com/#]http://cialisonla.com/[/url]
cialis coupons printable http://cialisonla.com/
[url=http://cialisonla.com/#]cialis without a doctors prescription[/url]
cialis rezeptfrei osterreichcialis on-line medicationviagra levitra cialis apcalis regalis zenegracialis 20 mg half lifeis there an alternative to cialis or viagraviagra cialis prescriptionduration of cialis
http://mobileformsoftware.com/__media__/js/netsoltrademark.php?d=cialiso...

cialis without a doctors prescription generic cialis lowest price cialisonla.com
tadalafil without a doctor's prescription [url=http://cialisonla.com/#]http://cialisonla.com/[/url]
cialis coupons printable http://cialisonla.com/
[url=http://cialisonla.com/#]cialis without a doctors prescription[/url]
cialis rezeptfrei osterreichcialis on-line medicationviagra levitra cialis apcalis regalis zenegracialis 20 mg half lifeis there an alternative to cialis or viagraviagra cialis prescriptionduration of cialis
http://mobileformsoftware.com/__media__/js/netsoltrademark.php?d=cialiso...

tadalafil without a doctor's prescription cialis tablets side effects cialissy.com
tadalafil 5mg preco http://cialissy.com/
generic for cialis 20mg http://cialissy.com/
tadalafil without a doctor's prescription
is it ok to mix viagra and cialiswhen cialis does not workcialis buitenlandaetna insurance cialiswe like it cialis soft gelcialis 10 mg tablet filmomhre viagra cialis levitracialis in patients with chf
http://ultafan.com/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without prescriptions cialis online pharmacy cialissy.com
cialis price walmart http://cialissy.com/
cialis from usa pharmacy http://cialissy.com/
cialis canada prescription
cialis and diabeticswhat is difference in viagra and cialiscan i take cialis one day and viagra the nextprices pill comparison cialis viagracialis soft samples onlinecialis soft prescription for womandouble dose of cialis
http://instamingle.org/__media__/js/netsoltrademark.php?d=cialissy.com

buy cialis no prescription generic cialis tadalafil uk cialissv.com
buy cialis http://cialissv.com/
cialis daily use http://cialissv.com/
cialis purchase online without prescription
ervaringen gebruik cialischeap cialis soft softabshow long does cialis erection lastcialis name brand cheapcialis retail price in ctco je lepsie viagra alebo cialiscialis retail
http://investretirement.com/__media__/js/netsoltrademark.php?d=cialissv.com

buy cialis no prescription generic cialis tadalafil uk cialissv.com
buy cialis http://cialissv.com/
cialis daily use http://cialissv.com/
cialis purchase online without prescription
ervaringen gebruik cialischeap cialis soft softabshow long does cialis erection lastcialis name brand cheapcialis retail price in ctco je lepsie viagra alebo cialiscialis retail
http://investretirement.com/__media__/js/netsoltrademark.php?d=cialissv.com

tadalafil without prescription cialis 20 mg cvs cialisonla.com
cialis lowest price 5mg http://cialisonla.com/
purchase tadalafil http://cialisonla.com/
get cialis prescription online
cialis caliscialis kopen in apotheekcialis rezeptfrei billig kaufen wog postmessage cialis subject remembertadalafil citrate cialiscialis 20 mg 8 comprimidos precioexperience with cialis
http://bookpig.org/__media__/js/netsoltrademark.php?d=cialisonla.com

cheap cialis without a doctors prescription tadalafil generic international cialisvi.com
tadalafil 5mg reviews http://cialisvi.com/
cialis prices 10mg http://cialisvi.com/
cialis without a doctor preion
online prescription cialis with discountsnitroglycerin and cialis softviagra v cialis questionssubaction showcomments cialis start from newestcialis and insomniacialis for blood pressure controlcialis generic 30 tabletsonline prescriptions cialiscialis online pharmacy scamsviagra vs. cialis little blue pill is losing big
http://printoptix.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cheap cialis without a doctors prescription tadalafil generic international cialisvi.com
tadalafil 5mg reviews http://cialisvi.com/
cialis prices 10mg http://cialisvi.com/
cialis without a doctor preion
online prescription cialis with discountsnitroglycerin and cialis softviagra v cialis questionssubaction showcomments cialis start from newestcialis and insomniacialis for blood pressure controlcialis generic 30 tabletsonline prescriptions cialiscialis online pharmacy scamsviagra vs. cialis little blue pill is losing big
http://printoptix.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis without a doctor's prescription tadalafil 5mg generic cialissi.com
cialis generic 2018 http://cialissi.com/
cialis cena srbija http://cialissi.com/
how to get cialis without doctor
generic cialis review ukque es mas seguro viagra o cialiscialis im internet kaufencialis and black menviagra works but cialis doeswhat is cialis levitraviagra vs cialis experience
http://poweradegames.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis without a doctor's prescription tadalafil 5mg generic cialissi.com
cialis generic 2018 http://cialissi.com/
cialis cena srbija http://cialissi.com/
how to get cialis without doctor
generic cialis review ukque es mas seguro viagra o cialiscialis im internet kaufencialis and black menviagra works but cialis doeswhat is cialis levitraviagra vs cialis experience
http://poweradegames.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis without a doctor's prescription tadalafil generic international cialisvv.com
cialis generico opinion http://cialisvv.com/
tadalafil generic dosage http://cialisvv.com/
cialis without a doctors prescription
viagra vs cialis betterbuy low cost cialis softlowest priced cialisnet cialisg postmessage cialis subject postviagra cialis celeritycialis buzzingmark martin cialis soft
http://eightlimbs.guru/__media__/js/netsoltrademark.php?d=cialisvv.com

cialis without a doctor's prescription tadalafil generic international cialisvv.com
cialis generico opinion http://cialisvv.com/
tadalafil generic dosage http://cialisvv.com/
cialis without a doctors prescription
viagra vs cialis betterbuy low cost cialis softlowest priced cialisnet cialisg postmessage cialis subject postviagra cialis celeritycialis buzzingmark martin cialis soft
http://eightlimbs.guru/__media__/js/netsoltrademark.php?d=cialisvv.com

cialis without a doctor's prescription usa tadalafil 10 mg daily cialisvi.com
cialis pills reviews http://cialisvi.com/
tadalafil tablets 40mg http://cialisvi.com/
cialis without a doctor's prescription
cialis tadalafil 100mgefficacy viagra vs cialiscialis mountains in prcialis 20 mg generiquecialis tadalafil icosdifferences entre viagra cialis levitradole cialis softcialis 5mg pricingprice cialis
http://bi-lahey-health-systems.com/__media__/js/netsoltrademark.php?d=ci...

cialis without a doctor's prescription usa tadalafil 10 mg daily cialisvi.com
cialis pills reviews http://cialisvi.com/
tadalafil tablets 40mg http://cialisvi.com/
cialis without a doctor's prescription
cialis tadalafil 100mgefficacy viagra vs cialiscialis mountains in prcialis 20 mg generiquecialis tadalafil icosdifferences entre viagra cialis levitradole cialis softcialis 5mg pricingprice cialis
http://bi-lahey-health-systems.com/__media__/js/netsoltrademark.php?d=ci...

cialis without a doctors prescription cialis generico online cialisvv.com
cialis lowest prices md pharmacy http://cialisvv.com/
cialis daily use http://cialisvv.com/
cialis no doctor's prescription
subaction showcomments cialis start from onlineviagra and cialis salestook 2 cialisviagra and cialis low pricecialis ecstacysnorting cialiscialis before afterdiscount cialis cannadacheap soft cialis 120 pillsis cialis like viagra
http://robcorddry.org/__media__/js/netsoltrademark.php?d=cialisvv.com

ed pills without a prescription cialis dosage cialisvi.com
cialis 20 mg coupon http://cialisvi.com/
purchase cialis online no prescription http://cialisvi.com/
cialis purchase online without prescription
het verschil tussen viagra en cialiscialis in apotheekcome funziona il cialis 20 mgco je lepsie viagra alebo cialis20 mg cialis dose adviceacheter du cialis sur internet forum
http://eltizoncigars.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis purchase online without prescription cialis condition cialissi.com
cialis uk suppliers http://cialissi.com/
buy cialis usa http://cialissi.com/
cialis purchase online without prescription
forum meglio viagra o cialiswhat men think of cialiscialis pillsbuy cialis delived next daycialis internet dangerwhat are the tub in cialis softcialis soft fedex shipping
http://ryanlandau.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis purchase online without prescription cialis condition cialissi.com
cialis uk suppliers http://cialissi.com/
buy cialis usa http://cialissi.com/
cialis purchase online without prescription
forum meglio viagra o cialiswhat men think of cialiscialis pillsbuy cialis delived next daycialis internet dangerwhat are the tub in cialis softcialis soft fedex shipping
http://ryanlandau.com/__media__/js/netsoltrademark.php?d=cialissi.com

viagra stories generic viagra canada buy viagra [url=https://niqabsquad.com/]canadian generic viagra[/url]

viagra pills when will viagra be generic viagra soft generic viagra india

viagra pills when will viagra be generic viagra soft generic viagra india

Pages

Add new comment