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

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

real money casino best online casino play casino gsn casino games | https://onlinecasino888.us.org/ - online gambling casino https://onlinecasino888.us.org/ - play casino

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

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

online casino gambling online casinos gsn casino slots casino blackjack | https://online-casinos.us.org/ - casino play https://online-casinos.us.org/ - betfair online casino

online casino gambling online casinos gsn casino slots casino blackjack | https://online-casinos.us.org/ - casino play https://online-casinos.us.org/ - betfair online casino

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

play casino free online casino slots best online casino real money casino | https://onlinecasino888.us.org/ - online casinos for us players https://onlinecasino888.us.org/ - gsn casino games

vegas slots online online slots real money free games online slots online slots | https://casinoslotsonline.us.org/ - free online slots vegas world https://casinoslotsonline.us.org/ - free online slots no download

vegas slots online online slots real money free games online slots online slots | https://casinoslotsonline.us.org/ - free online slots vegas world https://casinoslotsonline.us.org/ - free online slots no download

online casino gambling online casino real money online casino gambling gsn casino slots | https://online-casinos.us.org/ - play casino https://online-casinos.us.org/ - casino play

vegas casino slots online slots real money best online slots online slots real money | https://online-casinoslots.us.org/ - slots online https://online-casinoslots.us.org/ - gsn casino slots

vegas casino slots online slots real money best online slots online slots real money | https://online-casinoslots.us.org/ - slots online https://online-casinoslots.us.org/ - gsn casino slots

casino slots online slots real money world class casino slots vegas slots online | [url=https://casinoslotsonline.us.org/]slot games[/url] [url=https://casinoslotsonline.us.org/]vegas world free games online slots[/url]

casino slots online slots real money world class casino slots vegas slots online | [url=https://casinoslotsonline.us.org/]slot games[/url] [url=https://casinoslotsonline.us.org/]vegas world free games online slots[/url]

online casino bonus slots for real money best online casinos betfair online casino - https://onlinecasinoplay.us.org/ - betfair online casino - https://onlinecasinoplay.us.org/ - casino slots

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

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

recdiw fda

best online casino casino bonus casino real money betfair online casino | https://online-casinos.us.org/ - casino games https://online-casinos.us.org/ - real money casino

play slots casino slots free games online slots free online slots no download | https://online-casinoslots.us.org/ - free online slots vegas world https://online-casinoslots.us.org/ - gsn casino slots

play slots casino slots free games online slots free online slots no download | https://online-casinoslots.us.org/ - free online slots vegas world https://online-casinoslots.us.org/ - gsn casino slots

online casino online gambling best online casino online gambling casino | https://onlinecasino888.us.org/ - casino play https://onlinecasino888.us.org/ - casino real money

online casino online gambling best online casino online gambling casino | https://onlinecasino888.us.org/ - casino play https://onlinecasino888.us.org/ - casino real money

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

online casinos online casinos for us players casino bonus real money casino | https://online-casinos.us.org/ - casino play https://online-casinos.us.org/ - online casino gambling

online casinos online casinos for us players casino bonus real money casino | https://online-casinos.us.org/ - casino play https://online-casinos.us.org/ - online casino gambling

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

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

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

casino bonus casino online casino bonus play casino - https://onlinecasinoplay.us.org/ - best online casino - https://onlinecasinoplay.us.org/ - online casino gambling

casino bonus casino online casino bonus play casino - https://onlinecasinoplay.us.org/ - best online casino - https://onlinecasinoplay.us.org/ - online casino gambling

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

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

r viagra no prescription fda
viagra without prescription fda
http://starlite.id/viagra-without-a-doctor-prescription/# viagra no prescription

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

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

real money casino online casinos online gambling slots for real money - https://onlinecasinoplay.us.org/ - free online casino slots - https://onlinecasinoplay.us.org/ - play casino

real money casino online casinos online gambling slots for real money - https://onlinecasinoplay.us.org/ - free online casino slots - https://onlinecasinoplay.us.org/ - play casino

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

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

free games online slots online slot games vegas world free games online slots free online slots no download | https://slotsonliners.com/ - slots online https://slotsonliners.com/ - world class casino slots

free games online slots online slot games vegas world free games online slots free online slots no download | https://slotsonliners.com/ - slots online https://slotsonliners.com/ - world class casino slots

free online slots vegas world slots games free games online slots slots online | https://casinoslotstime.com/ - hollywood casino online slots https://casinoslotstime.com/ - hollywood casino online slots

free online slots vegas world slots games free games online slots slots online | https://casinoslotstime.com/ - hollywood casino online slots https://casinoslotstime.com/ - hollywood casino online slots

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

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

Pages

Add new comment