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
You Are Genius ..................
Do you have a working example/demo?
A variation of this is use on Cantorion.org.
Thanks! But, how do you keep the label from being submitted with a form. I'm using Ruby on Rails.
Keep a handle on all your WatermarkLabel objects (store them in an array if you have more than one):
var watermarks = [];
var w = new WatermarkLabel('input');
watermarks.push(w);
Then when you submit your form, simulate the focus event on each object:
form.onSubmit = function() {
watermarks.each(function(w) {
w.focus();
});
};
I found out something like this works:
function clearWaterMark()
{
var txt = document.getElementById(id_of_text_field);
if (txt.value == '[label]') txt.value = '';
}