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

online gambling real money casino casino blackjack bovada casino | https://onlinecasinoslotsmaxx.com/ - online casinos for us players https://onlinecasinoslotsmaxx.com/ - online casino

online gambling real money casino casino blackjack bovada casino | https://onlinecasinoslotsmaxx.com/ - online casinos for us players https://onlinecasinoslotsmaxx.com/ - online casino

best online casinos casino play online casino real money best online casino - https://onlinecasinoplay.us.org/ - online casinos - https://onlinecasinoplay.us.org/ - real money casino

online slots real casino slots turning stone online slots play slots | https://casinoslotstime.com/ - real casino slots https://casinoslotstime.com/ - casino slots

f how long does it take for viagra to work ed meds online without doctor prescription
alternative to viagra generic viagra without prescription
http://starlite.id/viagra-without-a-doctor-prescription/# buy viagra without prescription

f how long does it take for viagra to work ed meds online without doctor prescription
alternative to viagra generic viagra without prescription
http://starlite.id/viagra-without-a-doctor-prescription/# buy viagra without prescription

online slot games vegas slots online online slots real money best online slots | https://slotsonliners.com/ - vegas slots online https://slotsonliners.com/ - real casino slots

online slot games vegas slots online online slots real money best online slots | https://slotsonliners.com/ - vegas slots online https://slotsonliners.com/ - real casino slots

dxwdlu fda

dxwdlu fda

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

online casino slots online gambling online casino gambling online casino gambling | https://onlinecasinoslotsmaxx.com/ - slots for real money https://onlinecasinoslotsmaxx.com/ - online casinos

online gambling gsn casino games casino slots slots for real money - https://onlinecasinoplay.us.org/ - online gambling casino - https://onlinecasinoplay.us.org/ - online casino bonus

online slots vegas slots online turning stone online slots online slots real money | https://slotsonliners.com/ - free online slots https://slotsonliners.com/ - casino slots

vegas slots online online slots real money play slots online play slots online | https://casinoslotstime.com/ - free online slots vegas world https://casinoslotstime.com/ - cashman casino slots

vegas slots online online slots real money play slots online play slots online | https://casinoslotstime.com/ - free online slots vegas world https://casinoslotstime.com/ - cashman casino slots

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

gsn casino online casino bonus online gambling casino casino blackjack | https://onlinecasinoslotsmaxx.com/ - online casinos for us players https://onlinecasinoslotsmaxx.com/ - online casino slots

gsn casino online casino bonus online gambling casino casino blackjack | https://onlinecasinoslotsmaxx.com/ - online casinos for us players https://onlinecasinoslotsmaxx.com/ - online casino slots

turning stone online slots best online slots online slot games vegas casino slots | https://casinoslotstime.com/ - online slots https://casinoslotstime.com/ - online slots

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

f how long does viagra last ed meds online without doctor prescription
viagra no prescription viagra without doctor prescription
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without a doctor prescription

f how long does viagra last ed meds online without doctor prescription
viagra no prescription viagra without doctor prescription
http://starlite.id/viagra-without-a-doctor-prescription/# viagra without a doctor prescription

casino real money bovada casino casino real money casino real money | https://onlinecasinoslotsmaxx.com/ - real money casino https://onlinecasinoslotsmaxx.com/ - casino bonus

hollywood casino online slots cashman casino slots world class casino slots slots games | [url=https://casinoslotstime.com/]vegas casino slots[/url] [url=https://casinoslotstime.com/]online slots free[/url]

hollywood casino online slots cashman casino slots world class casino slots slots games | [url=https://casinoslotstime.com/]vegas casino slots[/url] [url=https://casinoslotstime.com/]online slots free[/url]

casino bonus play casino casino play play casino | https://onlinecasinoslotsmaxx.com/ - online casino bonus https://onlinecasinoslotsmaxx.com/ - slots for real money

casino bonus play casino casino play play casino | https://onlinecasinoslotsmaxx.com/ - online casino bonus https://onlinecasinoslotsmaxx.com/ - slots for real money

casino play casino bonus bovada casino best online casinos - https://onlinecasinoplay.us.org/ - online casino slots - https://onlinecasinoplay.us.org/ - mgm online casino

casino play casino bonus bovada casino best online casinos - https://onlinecasinoplay.us.org/ - online casino slots - https://onlinecasinoplay.us.org/ - mgm online casino

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

free online slots games real casino slots online slots free play slots | https://casinoslotstime.com/ - free games online slots https://casinoslotstime.com/ - online slots real money

free online slots games real casino slots online slots free play slots | https://casinoslotstime.com/ - free games online slots https://casinoslotstime.com/ - online slots real money

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

slots for real money best online casino gsn casino casino play | https://onlinecasinoslotsmaxx.com/ - bovada casino https://onlinecasinoslotsmaxx.com/ - casino bonus

c levitra without a doctor prescription ed meds online without doctor prescription
buy levitra levitra without a doctor prescription
http://starlite.id/levitra/# generic levitra vardenafil 20mg

c levitra without a doctor prescription ed meds online without doctor prescription
buy levitra levitra without a doctor prescription
http://starlite.id/levitra/# generic levitra vardenafil 20mg

online casino real money casino play play casino online casino slots | https://onlinecasinoslotsmaxx.com/ - online casino real money https://onlinecasinoslotsmaxx.com/ - mgm online casino

online casino real money casino play play casino online casino slots | https://onlinecasinoslotsmaxx.com/ - online casino real money https://onlinecasinoslotsmaxx.com/ - mgm online casino

bovada casino casino play gsn casino games casino online - https://onlinecasinoplay.us.org/ - gsn casino slots - https://onlinecasinoplay.us.org/ - casino games

bovada casino casino play gsn casino games casino online - https://onlinecasinoplay.us.org/ - gsn casino slots - https://onlinecasinoplay.us.org/ - casino games

online slots free online slots games slots online online slot games | https://casinoslots24.us.org/ - vegas casino slots https://casinoslots24.us.org/ - play slots online

free online slots vegas world vegas world free games online slots real casino slots slots online | https://casinoslotsplay.us.org/ - vegas world free games online slots https://casinoslotsplay.us.org/ - vegas world free games online slots

casino online free online casino slots online casinos best online casinos | https://onlinecasinoslotsmaxx.com/ - casino real money https://onlinecasinoslotsmaxx.com/ - online gambling casino

online casino casino games casino online online casinos - https://onlinecasinoplay.us.org/ - online casinos for us players - https://onlinecasinoplay.us.org/ - casino games

online casino casino games casino online online casinos - https://onlinecasinoplay.us.org/ - online casinos for us players - https://onlinecasinoplay.us.org/ - casino games

m ed meds online without doctor prescription levitra without a doctor prescription in usa
buy levitra generic levitra vardenafil 20mg
http://starlite.id/levitra/# levitra 20mg best price

m ed meds online without doctor prescription levitra without a doctor prescription in usa
buy levitra generic levitra vardenafil 20mg
http://starlite.id/levitra/# levitra 20mg best price

Pages

Add new comment