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 without a doctor's prescription cialis 5mg tablets cialissy.com
tadalafil 20 mg dosage http://cialissy.com/
tadalafil 5mg generic http://cialissy.com/
tadalafil without a doctor's prescription
capsule cialiscialis coumadinaugmentin cialismedicare cialis prescription drugs pharmacopeiacialis penile rehabilitation after rrpcialis bestellen.at
http://piranacmmsonline.com/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without a doctor's prescription usa cialis generic name cialissv.com
cialis prices 10mg http://cialissv.com/
tadalafil generic dosage http://cialissv.com/
cialis for daily use without prescription
viagra vs cialis headachesbuying generic cialiscialis rezeptfrei kaufen paypalwat doet cialis bij vrouwenbuy cialis soft online without dr approvalcialis 5 mg instructionscialis viagra levitra quale il migliorecialis levitra kaufencialis clone
http://wlrn.digital/__media__/js/netsoltrademark.php?d=cialissv.com

ed pills without a prescription generic cialis online cialissy.com
tadalafil http://cialissy.com/
tadalafil side effects http://cialissy.com/
cialis without a doctors prescription
kann man cialis ohne rezept in der apotheke kaufencialis online pharmacy australiacialis in argentinacialis cant cumfacts on cialiscialis dose 30mgcialis generika online apothekeuse of cialis in dogs
http://oemdodgepart.com/__media__/js/netsoltrademark.php?d=cialissy.com

buy cialis without a doctor's tadalafil side effects cialisvi.com
cialis 5mg coupon http://cialisvi.com/
generic for cialis or viagra http://cialisvi.com/
cialis purchase online without prescription
cialis south africapatanol cialis nexiumvendita cialiscialis soft dosageviagra cialis bu safe paypalcost comparison of viagra and cialis
http://mineriabreca.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis prescription buy cialis online no prescription cialisvv.com
cialis 20mg use http://cialisvv.com/
tadalafil 5mg reviews http://cialisvv.com/
online cialis no prescription
rx cialisno prescription cialis overnight deliveryeurope online sale cialis softbuy cialis soft drugcialis 5mg preis apotheke
http://mujerdevisionint.org/__media__/js/netsoltrademark.php?d=cialisvv.com

You are my inspiration, I possess few blogs and sometimes run out
from post :).

Also visit my web-site ... key - onlinemanuals.txdot.gov
-

cialis prescription buy cialis online no prescription cialisvv.com
cialis 20mg use http://cialisvv.com/
tadalafil 5mg reviews http://cialisvv.com/
online cialis no prescription
rx cialisno prescription cialis overnight deliveryeurope online sale cialis softbuy cialis soft drugcialis 5mg preis apotheke
http://mujerdevisionint.org/__media__/js/netsoltrademark.php?d=cialisvv.com

tadalafil without a doctor's prescription cialis online canada cialisonli.com
tadalafil 20mg tab http://cialisonli.com/
cialis lowest price coupon http://cialisonli.com/
cialis purchase online without prescription
fa piu effetto il viagra o il cialiscompare cialis priceexperience with generic cialiscialis for oncecialis soft on sale cheap onlinewhere to buy cialis in ontariodora cialis soft
http://xlsynergy.com/__media__/js/netsoltrademark.php?d=cialisonli.com

cialis without a doctor's prescription buy generic cialis cialissi.com
buy cheap cialis on line http://cialissi.com/
cialis 20 mg coupons 2018 http://cialissi.com/
cialis without a doctors prescription
cialis kaufen expresscialis ohne rezept in hollandcialis viagra nediraverage cost of cialis softgeneric cialis new zealandgeneric cialis money order
http://mcdonoughfirearms.com/__media__/js/netsoltrademark.php?d=cialissi...

cialis without a doctors prescription cialis cena apoteka cialisonli.com
cialis uk cost http://cialisonli.com/
tadalafil 20 mg troche http://cialisonli.com/
cheap cialis without prescription
cialis pillendienstcialis reviews drugs.commy husband took cialiscialis generic kopencialis kamagra levitramexico cialisonline purchase of cialisalternative to cialis soft onlinelilly icos cialis pills
http://tonyfrancisco.com/__media__/js/netsoltrademark.php?d=cialisonli.com

cialis without a doctors prescription cialis cena apoteka cialisonli.com
cialis uk cost http://cialisonli.com/
tadalafil 20 mg troche http://cialisonli.com/
cheap cialis without prescription
cialis pillendienstcialis reviews drugs.commy husband took cialiscialis generic kopencialis kamagra levitramexico cialisonline purchase of cialisalternative to cialis soft onlinelilly icos cialis pills
http://tonyfrancisco.com/__media__/js/netsoltrademark.php?d=cialisonli.com

buy cialis without a doctor's cialis generico en mexico cialisvv.com
tadalafil 10 mg daily http://cialisvv.com/
cheap cialis http://cialisvv.com/
cialis without a doctor's prescription
cialis side effect indigestioncialis soft 20 mggenerika cialis kaufen paypaleffet du cialis sur les femmeswhere to purchase viagra cialis levitracialis pharmacy paxilkamagra cialisveilig online cialis kopencelecoxib generic medications discount pharmacy cialis
http://mybdl.com/__media__/js/netsoltrademark.php?d=cialisvv.com

buy cialis without a doctor's cialis generico en mexico cialisvv.com
tadalafil 10 mg daily http://cialisvv.com/
cheap cialis http://cialisvv.com/
cialis without a doctor's prescription
cialis side effect indigestioncialis soft 20 mggenerika cialis kaufen paypaleffet du cialis sur les femmeswhere to purchase viagra cialis levitracialis pharmacy paxilkamagra cialisveilig online cialis kopencelecoxib generic medications discount pharmacy cialis
http://mybdl.com/__media__/js/netsoltrademark.php?d=cialisvv.com

buy cialis online without prescription tadalafil 10 mg troche cialissi.com
buy cialis online safely http://cialissi.com/
cialis 5 mg price walmart http://cialissi.com/
cialis without a doctors prescription
cialis internet apotheekcialis pharmacy directgenerieke cialis kopencialis da 5 mg prezzo in farmaciacialis mt tadalafilcialis de 20 mg dosiswhat is the difference in viagra levitra and cialiscialis generic mt tadalafil
http://upliftfl.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis without a doctors prescription tadalafil 20mg for sale cialissv.com
cialis tadalafil c200 http://cialissv.com/
buy cheap cialis on line http://cialissv.com/
cialis online without prescriptio
real cialis soft womancialis vs viagra opinionscialis 20 refillque es mas potente el viagra o el cialiscialis long term side effectare viagra and cialis covered by insurancecialis en andere medicijnenqual o melhor viagra levitra ou cialis
http://7ashta5.com/__media__/js/netsoltrademark.php?d=cialissv.com

Everything published was actually very logical. But, think
about this, what if you added a little content?
I am not saying your content is not good., however what if you added a title
that makes people desire more? I mean Add new comment | Semplice is a
little boring. You should look at Yahoo's home page and watch how they create article
headlines to grab viewers to open the links. You might add a related video
or a related pic or two to get readers excited about everything've
got to say. In my opinion, it could bring your
posts a little bit more interesting.

Here is my web site ... key (http://aggiesatweb.tamu.edu/)

cialis without a doctors prescription tadalafil 20mg for sale cialissv.com
cialis tadalafil c200 http://cialissv.com/
buy cheap cialis on line http://cialissv.com/
cialis online without prescriptio
real cialis soft womancialis vs viagra opinionscialis 20 refillque es mas potente el viagra o el cialiscialis long term side effectare viagra and cialis covered by insurancecialis en andere medicijnenqual o melhor viagra levitra ou cialis
http://7ashta5.com/__media__/js/netsoltrademark.php?d=cialissv.com

cialis purchase online without prescription cialis pills cialissi.com
generic cialis tadalafil uk http://cialissi.com/
cialis 5mg prix http://cialissi.com/
by cialis without prescription
daily cialis pillbuying cialis soft ukwhich works best cialis or viagrageorgia health insurance coverage cialiscialis vs viagra user reviewscanadian pharmacy cialis no prescriptioncialis soft online no prescriptioncialis generika erfahrungsberichte
http://northbergenparkingauthority.com/__media__/js/netsoltrademark.php?...

I conceive you have observed some very interesting points, thanks
for the post.

Visit my page ... mac (http://www.ega.edu/?URL=is.gd/UwePU9)

Wonderful work! This is the type of info that should be shared across the net.
Shame on the seek engines for no longer positioning this publish upper!
Come on over and consult with my website . Thanks =)

my web page :: mac (Velva)

tadalafil online without a prescription cialis prices walmart cialisvi.com
cialis uk http://cialisvi.com/
generic cialis 20mg http://cialisvi.com/
tadalafil without a doctor's prescription
cialis outdatedwhere to order cialis in mexicoviagra vs cialis erectiononline apotheke holland cialisdiferencia viagra y cialisviagra and cialis mixingtadalafil 20 mg
http://sw638.com/__media__/js/netsoltrademark.php?d=cialisvi.com

I don't usually comment but I gotta state regards for the post on this amazing one
:D.

My web-site :: key (Barb)

Hi! Someone in my Facebook group shared this site with us so I came to look it over.

I'm definitely enjoying the information. I'm book - a.pr-cy.ru,-marking and will be
tweeting this to my followers! Superb blog and terrific design and style.

tadalafil online without a prescription cialis prices walmart cialisvi.com
cialis uk http://cialisvi.com/
generic cialis 20mg http://cialisvi.com/
tadalafil without a doctor's prescription
cialis outdatedwhere to order cialis in mexicoviagra vs cialis erectiononline apotheke holland cialisdiferencia viagra y cialisviagra and cialis mixingtadalafil 20 mg
http://sw638.com/__media__/js/netsoltrademark.php?d=cialisvi.com

by cialis without prescription generic cialis tadalafil india cialissv.com
buy cialis http://cialissv.com/
cialis 5mg generic prices http://cialissv.com/
cialis purchase online without prescription
viagra and cialis on salecialis powered by phpbbcialis rezeptfrei bestellen erfahrungencialis for bphdifference between viagra and cialiscialis 10mg filmtabletten preiscialis 5 mg 14 compresse prezzo
http://mymauiconnection.com/__media__/js/netsoltrademark.php?d=cialissv.com

tadalafil without prescription cialis 5mg coupon cialissi.com
cialis 5 mg coupon http://cialissi.com/
buy cialis online no prescription http://cialissi.com/
tadalafil without a doctor's prescription
cialis recreational use whywhat is the difference between viagra & cialisdoes cialis come in ginaricgeneric cialis from india warningside effects viagra cialiscombined viagra cialis ed urologycialis ohne rezept erfahrungenisraeli fighter cialis
http://txheritage.com/__media__/js/netsoltrademark.php?d=cialissi.com

tadalafil without prescription cialis 5mg coupon cialissi.com
cialis 5 mg coupon http://cialissi.com/
buy cialis online no prescription http://cialissi.com/
tadalafil without a doctor's prescription
cialis recreational use whywhat is the difference between viagra & cialisdoes cialis come in ginaricgeneric cialis from india warningside effects viagra cialiscombined viagra cialis ed urologycialis ohne rezept erfahrungenisraeli fighter cialis
http://txheritage.com/__media__/js/netsoltrademark.php?d=cialissi.com

cialis without prescription cialis lowest price cialissy.com
cialis tadalafil 5mg http://cialissy.com/
cialis tablets for sale http://cialissy.com/
cialis without a doctors prescription
viagra or cialis pricesbuy online prescription cialis soft withoutcialis 8 cpr riv 10mgcombined cialis viagradiscount levitra cialis viagracheap cialis soft tadalafil 20 mgcompra cialisviagra vs cialis dailygeneric cialis levitracialis zollfrei bestellen
http://clicksbilliards.info/__media__/js/netsoltrademark.php?d=cialissy.com

Way cool! Some extremely valid points! I appreciate you penning this post plus the rest of the website is really good.

My webpage - book (Marlys)

cialis without prescription cialis lowest price cialissy.com
cialis tadalafil 5mg http://cialissy.com/
cialis tablets for sale http://cialissy.com/
cialis without a doctors prescription
viagra or cialis pricesbuy online prescription cialis soft withoutcialis 8 cpr riv 10mgcombined cialis viagradiscount levitra cialis viagracheap cialis soft tadalafil 20 mgcompra cialisviagra vs cialis dailygeneric cialis levitracialis zollfrei bestellen
http://clicksbilliards.info/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without a doctors prescription in usa cheap cialis online cialisvi.com
buy cialis germany http://cialisvi.com/
buy generic cialis http://cialisvi.com/
cialis without a doctor's prescription in usa
low libido cialis soft high blood pressurec-pill cheap cialiscan you cut cialis in halftadalafilside effects of cialiscialis pills couponbuy viagra plus cialis for $69cialis online discoun
http://techafaqs.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis without a doctors prescription in usa cheap cialis online cialisvi.com
buy cialis germany http://cialisvi.com/
buy generic cialis http://cialisvi.com/
cialis without a doctor's prescription in usa
low libido cialis soft high blood pressurec-pill cheap cialiscan you cut cialis in halftadalafilside effects of cialiscialis pills couponbuy viagra plus cialis for $69cialis online discoun
http://techafaqs.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis without a doctor's prescription tadalafil generic 20mg cialisonli.com
cialis generico en farmacias similares http://cialisonli.com/
buy cheap cialis coupon http://cialisonli.com/
cialis without a doctor
cialis wat is datcialis online no presriptionmenopause cialiscialis and grapfruitcialis dosagescialis italiawomans cialis softbuying brand cialis online
http://ratfuckers.com/__media__/js/netsoltrademark.php?d=cialisonli.com

cialis purchase online without prescription buy cialis pills cialisonla.com
tadalafil 10 mg 180 pills [url=http://cialisonla.com/#]http://cialisonla.com/[/url]
cialis 20mg usage http://cialisonla.com/
[url=http://cialisonla.com/#]cialis purchase online without prescription[/url]
cialis cialis genuinerx netcompare dosage of cialis to viagrafree samples of cialiscompare cheap generic cialistub symbolism in cialis ads
http://facechairs.com/__media__/js/netsoltrademark.php?d=cialisonla.com

cialis purchase online without prescription buy cialis pills cialisonla.com
tadalafil 10 mg 180 pills [url=http://cialisonla.com/#]http://cialisonla.com/[/url]
cialis 20mg usage http://cialisonla.com/
[url=http://cialisonla.com/#]cialis purchase online without prescription[/url]
cialis cialis genuinerx netcompare dosage of cialis to viagrafree samples of cialiscompare cheap generic cialistub symbolism in cialis ads
http://facechairs.com/__media__/js/netsoltrademark.php?d=cialisonla.com

cialis without doctor prescription tadalafil 20mg for sale cialissi.com
generic cialis available http://cialissi.com/
tadalafil tablets 60 mg http://cialissi.com/
cialis online without prescription
achat cialis en italique es mas fuerte viagra o cialiswhat does street cialis look likelevitra vs viagra vs cialiscialis substitutes over the countercialis softabsalternate between viagra and cialiscialis identificationquestions and answers about 5mg cialis
http://wellnesswithadriennetotino.com/__media__/js/netsoltrademark.php?d...

cialis without doctor prescription tadalafil 20mg for sale cialissi.com
generic cialis available http://cialissi.com/
tadalafil tablets 60 mg http://cialissi.com/
cialis online without prescription
achat cialis en italique es mas fuerte viagra o cialiswhat does street cialis look likelevitra vs viagra vs cialiscialis substitutes over the countercialis softabsalternate between viagra and cialiscialis identificationquestions and answers about 5mg cialis
http://wellnesswithadriennetotino.com/__media__/js/netsoltrademark.php?d...

tadalafil without a doctor's prescription cialis 20mg price cialissi.com
cheap cialis online http://cialissi.com/
cialis tadalafil 5mg http://cialissi.com/
cialis without a doctor's prescription
diltiazem and cialisgeneric cialis uk online pharmacyprice cialis mexico pharmacycialis tadalafil cialispiu potente cialis o viagra
http://spoton-deals.com/__media__/js/netsoltrademark.php?d=cialissi.com

I love what you guys are up too. This type of clever work and
coverage! Keep up the good works guys I've included you guys to blogroll.

Feel free to visit my web page ... window (fore.yale.edu)

cialis without a doctor's prescription cialis prices cialisonla.com
cialis can cause cataracts http://cialisonla.com/
cialis purchase online without prescription http://cialisonla.com/
cialis without a doctor's prescription
canadian pharmacies cialis softdifference viagra and cialisconstruction automobile presse sp cialis ecialis europe cheap viagracialis drug for impotencecialis ohne rezept forumcialis kopen rotterdamcialis fast shipping
http://aadhayainformationsolutions.com/__media__/js/netsoltrademark.php?...

cialis without a doctors prescription cialis tablets generic cialisvi.com
tadalafil 20mg troche http://cialisvi.com/
cialis 20 mg best price http://cialisvi.com/
tadalafil without a doctors prescription
cialis generic online from canadacialis informaci ncialis rezeptfrei kaufen hollandcialis prices st louiscialis generika in europa kaufen
http://chancingfate.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis purchase online without prescription cialis generic availability cialisvi.com
generico de cialis http://cialisvi.com/
cialis generico en mexico http://cialisvi.com/
how to get cialis without doctor
cialis viagra para mujercialis ohne rezept in osterreichcialis vitamin cannuaire sp cialis tourismeoutillage sp cialis
http://filterspecialistsinc.com/__media__/js/netsoltrademark.php?d=ciali...

As I site possessor I believe the content matter here is rattling wonderful ,
appreciate it for your hard work. You should keep it
up forever! Best of luck.

Here is my page ... window (www.mcdaniel.edu)

cialis purchase online without prescription cialis generic availability cialisvi.com
generico de cialis http://cialisvi.com/
cialis generico en mexico http://cialisvi.com/
how to get cialis without doctor
cialis viagra para mujercialis ohne rezept in osterreichcialis vitamin cannuaire sp cialis tourismeoutillage sp cialis
http://filterspecialistsinc.com/__media__/js/netsoltrademark.php?d=ciali...

cialis without a doctor's prescription cialis lowest price cialissy.com
tadalafil 20mg tab http://cialissy.com/
purchase tadalafil http://cialissy.com/
cialis without a doctors prescription
cialis generic availabilitycialis viagria levitracialis soft interactionsdiscount cialis soft europebenefits of cialis softbest pharmacy cialis softcialis vs viagra safety1buy canada cialiscialis 10mg filmtabletten bestellencialis once a day effectiveness
http://godfreymagician.com/__media__/js/netsoltrademark.php?d=cialissy.com

cialis without prescription buy cialis online no prescription cialisvi.com
cialis prices 20mg http://cialisvi.com/
generic cialis 20mg http://cialisvi.com/
cialis without doctor prescription
diferencias entre el viagra cialis or levitracialis internet erfahrungenwhat drug class is cialiscialis dosebuy brand name cialis
http://ntizen.com/__media__/js/netsoltrademark.php?d=cialisvi.com

cialis without prescription buy cialis online no prescription cialisvi.com
cialis prices 20mg http://cialisvi.com/
generic cialis 20mg http://cialisvi.com/
cialis without doctor prescription
diferencias entre el viagra cialis or levitracialis internet erfahrungenwhat drug class is cialiscialis dosebuy brand name cialis
http://ntizen.com/__media__/js/netsoltrademark.php?d=cialisvi.com

I got what you intend,saved to my bookmarks, very nice site.

Feel free to surf to my site - window (https://www.alexa.com/siteinfo/couponpromohot.com)

cialis without a doctor's prescription canada generic cialis lowest price cialisonli.com
cialis prices 5mg http://cialisonli.com/
buy cialis pills http://cialisonli.com/
buy cialis without a prescription
over the counter cialis soft substituteviagra vs cialis levitraviagra cialis levitra trial packcialis mvpcialis pain penismagazine sp cialiswhat is cialis and levitraregular cialis vs daily cialis
http://vipclaseejecutiva.com/__media__/js/netsoltrademark.php?d=cialison...

Wow, superb blog format! How lengthy have you been running a blog
for? you made running a blog glance easy. The total glance of
your site is excellent, let alone the content material![X-N-E-W-L-I-N-S-P-I-N-X]I just
could not leave your website before suggesting that I really enjoyed the standard
information an individual provide to your visitors?
Is going to be again continuously to investigate
cross-check new posts.

Feel free to visit my blog post - norton symantec (installic.com)

Pages

Add new comment