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 slots free cashman casino slots gold fish casino slots world class casino slots | https://casinoslotsplay.us.org/ - free online slots vegas world https://casinoslotsplay.us.org/ - gsn casino slots

online slots free cashman casino slots gold fish casino slots world class casino slots | https://casinoslotsplay.us.org/ - free online slots vegas world https://casinoslotsplay.us.org/ - gsn casino slots

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

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

betfair online casino casino play best online casino online casino games - https://onlinecasinoplay.us.org/ - casino games - https://onlinecasinoplay.us.org/ - gsn casino

betfair online casino casino play best online casino online casino games - https://onlinecasinoplay.us.org/ - casino games - https://onlinecasinoplay.us.org/ - gsn casino

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

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

casino play online casino bonus mgm online casino online casino | https://onlinecasinoplay777.us.org/ - online gambling casino https://onlinecasinoplay777.us.org/ - bovada casino

casino play online casino bonus mgm online casino online casino | https://onlinecasinoplay777.us.org/ - online gambling casino https://onlinecasinoplay777.us.org/ - bovada casino

mgm online casino casino games gsn casino games play casino | https://online-casinos.us.org/ - online casino https://online-casinos.us.org/ - online casino gambling

mgm online casino casino games gsn casino games play casino | https://online-casinos.us.org/ - online casino https://online-casinos.us.org/ - online casino gambling

best online casinos
casino games
casino blackjack
https://onlinecasinoplay.us.org/ - betfair online casino
https://onlinecasinoplay.us.org/ - online casinos
https://onlinecasinoplay.us.org/ betfair online casino

best online casinos
casino games
casino blackjack
https://onlinecasinoplay.us.org/ - betfair online casino
https://onlinecasinoplay.us.org/ - online casinos
https://onlinecasinoplay.us.org/ betfair online casino

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

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

online casino gambling free online casino slots online casino bonus gsn casino - https://onlinecasinoplay.us.org/ - online casino bonus - https://onlinecasinoplay.us.org/ - online casinos

online casino gambling free online casino slots online casino bonus gsn casino - https://onlinecasinoplay.us.org/ - online casino bonus - https://onlinecasinoplay.us.org/ - online casinos

gsn casino games gsn casino free online casino slots online casino slots | https://onlinecasinoplay777.us.org/ - casino online https://onlinecasinoplay777.us.org/ - mgm online casino

betfair online casino online gambling casino online casino bonus online casino gambling - [url=https://onlinecasinoplay.us.org/]casino play[/url] - [url=https://onlinecasinoplay.us.org/]casino bonus[/url]

betfair online casino online gambling casino online casino bonus online casino gambling - [url=https://onlinecasinoplay.us.org/]casino play[/url] - [url=https://onlinecasinoplay.us.org/]casino bonus[/url]

online gambling casino bovada casino casino slots online casino gambling | https://onlinecasino888.us.org/ - online casino games https://onlinecasino888.us.org/ - casino games

slots for real money casino games casino slots free online casino slots - https://onlinecasinoplay.us.org/ - bovada casino - https://onlinecasinoplay.us.org/ - casino online

slots for real money casino games casino slots free online casino slots - https://onlinecasinoplay.us.org/ - bovada casino - https://onlinecasinoplay.us.org/ - casino online

gold fish casino slots free online slots vegas world slots games free online slots games | https://online-casinoslots.us.org/ - turning stone online slots https://online-casinoslots.us.org/ - online slots

gold fish casino slots free online slots vegas world slots games free online slots games | https://online-casinoslots.us.org/ - turning stone online slots https://online-casinoslots.us.org/ - online slots

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

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

hollywood casino online slots slot games play slots online real casino slots | https://casinoslotsonline.us.org/ - free online slots vegas world https://casinoslotsonline.us.org/ - vegas world free games online slots

hollywood casino online slots slot games play slots online real casino slots | https://casinoslotsonline.us.org/ - free online slots vegas world https://casinoslotsonline.us.org/ - vegas world free games online slots

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

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

online gambling casino casino games mgm online casino free online casino slots | https://onlinecasino888.us.org/ - casino real money https://onlinecasino888.us.org/ - online gambling

vegas world free games online slots free online slots games gold fish casino slots vegas slots online | https://online-casinoslots.us.org/ - hollywood casino online slots https://online-casinoslots.us.org/ - slots online

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

best online slots free games online slots turning stone online slots gold fish casino slots | https://casinoslotsonline.us.org/ - play slots https://casinoslotsonline.us.org/ - slots online

best online slots free games online slots turning stone online slots gold fish casino slots | https://casinoslotsonline.us.org/ - play slots https://casinoslotsonline.us.org/ - slots online

best online casinos online casinos play casino online casino bonus | https://onlinecasino888.us.org/ - online casinos https://onlinecasino888.us.org/ - online casino bonus

best online casinos online casinos play casino online casino bonus | https://onlinecasino888.us.org/ - online casinos https://onlinecasino888.us.org/ - online casino bonus

free games online slots play slots play slots online vegas slots online | https://online-casinoslots.us.org/ - free online slots https://online-casinoslots.us.org/ - online slots free

free games online slots play slots play slots online vegas slots online | https://online-casinoslots.us.org/ - free online slots https://online-casinoslots.us.org/ - online slots free

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

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

best online casinos casino play online casino slots betfair online casino | https://onlinecasino888.us.org/ - play casino https://onlinecasino888.us.org/ - online casino bonus

best online casinos casino play online casino slots betfair online casino | https://onlinecasino888.us.org/ - play casino https://onlinecasino888.us.org/ - online casino bonus

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

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

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

Pages

Add new comment