citrusbyte / jquery-watermark

Add a watermark to your form elements – the jQuery way.

This URL has Read+Write access

xetorthio (author)
Tue Nov 03 09:06:23 -0800 2009
djanowski (committer)
Tue Nov 03 10:19:41 -0800 2009
jquery-watermark / ui.watermark.js
100644 58 lines (46 sloc) 1.23 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* jQuery Form Watermark
*
* Author: Damian Janowski
* E-mail: damian.janowski@citrusbyte.com
*/
 
$.widget("ui.watermark", {
  _init: function() {
    var watermark = this;
 
    var form = this.element;
 
    this.elements = $("input:text, input:password, textarea", form);
 
    this.elements.focus(this.handlerOff);
    this.elements.blur(this.handlerOn);
 
    this.refresh();
 
    $(form).submit(function() {
      watermark.elements.each(watermark.handlerOff);
      return true;
    });
  },
 
  refresh: function() {
    this.elements.each(this.handlerOn);
  },
 
  handlerOn: function() {
    if (this.title && this.title != '' && (this.value == '' || this.value == this.title)) {
      $(this).addClass("watermark");
      this.value = this.title;
      if(this.type == "password") {
        this.isPassword = true;
        this.type = "text";
      }
    }
 
    return true;
  },
 
  handlerOff: function() {
    if (this.type == 'text' || this.type == 'textarea') {
      if (this.value == this.title && this.title && this.title != '') {
        this.value = '';
        $(this).removeClass("watermark");
        if(this.isPassword) {
          this.type = "password";
        }
      }
    }
 
    return true;
  }
});