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

bovada casino online casino bonus gsn casino slots casino bonus - https://onlinecasinoplay.us.org/ - free online casino slots - https://onlinecasinoplay.us.org/ - casino blackjack

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

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

free online slots no download no registration slots online online slots slots online | https://casinoslotstime.com/ - free slots vegas world https://casinoslotstime.com/ - casino slots

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

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

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

free penny slots real casino slots real casino slots vegas free slots | https://slotsonliners.com/ - online slots free https://slotsonliners.com/ - casino slots free

free online casino slots mgm online casino casino online online casino slots | https://onlinecasinoplay777.us.org/ - play casino https://onlinecasinoplay777.us.org/ - online casino bonus

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

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

free online slots games slot machines vegas world free games online slots cashman casino slots | https://slotsonliners.com/ - turning stone online slots https://slotsonliners.com/ - vegas slots online

play slots online gsn casino slots best online slots free online slots | https://casinoslotstime.com/ - slot games https://casinoslotstime.com/ - play slots

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

casino real money casino games online casinos online gambling casino - https://onlinecasinoplay.us.org/ - online casino real money - https://onlinecasinoplay.us.org/ - online casino real money

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

gsn casino slots play slots cashman casino slots online slots real money | https://slotsonliners.com/ - cashman casino slots https://slotsonliners.com/ - gold fish casino slots

gsn casino slots play slots cashman casino slots online slots real money | https://slotsonliners.com/ - cashman casino slots https://slotsonliners.com/ - gold fish casino slots

turning stone online slots slots games best online slots online slots real money | https://casinoslotstime.com/ - online slots real money https://casinoslotstime.com/ - gold fish casino slots

casino real money online gambling play casino online casino bonus - https://onlinecasinoplay.us.org/ - online casinos - https://onlinecasinoplay.us.org/ - online casino games

casino real money online gambling play casino online casino bonus - https://onlinecasinoplay.us.org/ - online casinos - https://onlinecasinoplay.us.org/ - online casino games

casino online betfair online casino online gambling casino betfair online casino | https://onlinecasinoplay777.us.org/ - real money casino https://onlinecasinoplay777.us.org/ - online casino games

casino online betfair online casino online gambling casino betfair online casino | https://onlinecasinoplay777.us.org/ - real money casino https://onlinecasinoplay777.us.org/ - online casino games

online casino bovada casino play casino play casino | https://online-casinos.us.org/ - best online casino https://online-casinos.us.org/ - online gambling

gsn casino slots mgm online casino best online casino best online casinos | https://online-casinos.us.org/ - casino online https://online-casinos.us.org/ - best online casino

world class casino slots free games online slots turning stone online slots play slots online | https://casinoslots24.us.org/ - free games online slots https://casinoslots24.us.org/ - free online slots no download

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

slots online best online slots casino slots vegas casino slots | https://casinoslotsplay.us.org/ - online slots free https://casinoslotsplay.us.org/ - online slot games

slots online best online slots casino slots vegas casino slots | https://casinoslotsplay.us.org/ - online slots free https://casinoslotsplay.us.org/ - online slot games

online casino slots online gambling casino casino real money online casino bonus | https://onlinecasinoplay777.us.org/ - best online casino https://onlinecasinoplay777.us.org/ - gsn casino slots

casino online online casino gambling gsn casino slots gsn casino slots | https://online-casinos.us.org/ - bovada casino https://online-casinos.us.org/ - best online casinos

casino blackjack casino play online casino gambling casino real money | [url=https://online-casinos.us.org/]free online casino slots[/url] [url=https://online-casinos.us.org/]real money casino[/url]

casino blackjack casino play online casino gambling casino real money | [url=https://online-casinos.us.org/]free online casino slots[/url] [url=https://online-casinos.us.org/]real money casino[/url]

online casino bonus bovada casino gsn casino online gambling casino - https://onlinecasinoplay.us.org/ - casino slots - https://onlinecasinoplay.us.org/ - casino blackjack

online casino bonus bovada casino gsn casino online gambling casino - https://onlinecasinoplay.us.org/ - casino slots - https://onlinecasinoplay.us.org/ - casino blackjack

online casino slots cashman casino slots best online slots hollywood casino online slots | https://casinoslots24.us.org/ - free online slots games https://casinoslots24.us.org/ - gold fish casino slots

online casino slots cashman casino slots best online slots hollywood casino online slots | https://casinoslots24.us.org/ - free online slots games https://casinoslots24.us.org/ - gold fish casino slots

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

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

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

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

online casinos for us players slots for real money online casino best online casino | [url=https://onlinecasinoplay777.us.org/]online casino gambling[/url] [url=https://onlinecasinoplay777.us.org/]bovada casino[/url]

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

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

Pages

Add new comment