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;
		this.textbox.observe('focus', this.focus.bindAsEventListener(this));
		this.textbox.observe('blur', this.blur.bindAsEventListener(this));
	focus: function(event) {
			this.textbox.value = '';
		this.empty = false;
	blur: function(event) {
		if(this.textbox.value == '') {
			this.textbox.value = this.text;
			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:


All scripts are Public Domain.


canada pharmacy online no script https://onlinepharmacyero.com you could try here [url=https://onlinepharmacyero.com]onlinepharmacyero.com[/url] what was viagra originally created for canada pharmacy online no script

side effects of viagra https://onlinepharmacyero.com rx pharmacy

side effects of viagra https://onlinepharmacyero.com rx pharmacy

Absolutely NEW update of captchas breaking software "XEvil 4.0":
captcha solving of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
and more than 8400 another categories of captcha,
with highest precision (80..100%) and highest speed (100 img per second).
You can use XEvil 4.0 with any most popular SEO/SMM software: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

Interested? You can find a lot of introducing videos about XEvil in YouTube.

Free XEvil Demo available.

Good luck ;)


female viagra cvs https://onlinepharmacyero.com canada pharmacy

canada pharmacy online no script https://onlinepharmacyero.com browse around this web-site [url=https://onlinepharmacyero.com]Online Pharmacy Ero[/url] viagra red face canada pharmacy

canada drugs https://onlinepharmacyero.com check out here i loved this viagra sildenafil citrate prescription drugs online without doctor

займ залог птс Херсонская улица деньги в залог птс в москве autolombard one банки которые дают деньги под залог птс https://mailssents.com займ по птс магнитогорск

navigate to this website online pharmacy viagra mg

navigate to this website online pharmacy viagra mg

online pharmacy https://onlinepharmacyero.com anonymous Online Pharmacy Ero viagra works useful content

viagra warning label https://favoritedrstore.com viagra without a doctor prescription

why viagra doesnt work https://favoritedrstore.com viagra without a doctor prescription

navigate to this web-site viagra20c.com generic viagra paypal

over at this website pfizer generic viagra 50 mg viagra

viagra coupons for cvs https://favoritedrstore.com generic viagra 100mg

is two year old viagra still good https://viagra20c.com generic viagra without a doctor prescription

This Site cheap viagra viagra 100mg how long does it last

visit site our website how much bigger does viagra make you

generic viagra side effects https://viagra20c.com generic viagra online pharmacy

viagra gif https://favoritedrstore.com viagra generic

click to investigate click over here now viagra dick

click here to read generic viagra without a doctor prescription is there a generic form of viagra


Add new comment