-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.defaultBlur.js
96 lines (91 loc) · 3.44 KB
/
jquery.defaultBlur.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// jQuery defaultBlur - v1.1 - 4/19/2010
// http://ralphwhitbeck.com
//
// Copyright (c) 2010 Ralph Whitbeck
// Dual licensed under the MIT and GPL licenses.
//
// Description: use to make a input/textarea have a setable default
// value and a setable class that act as a label then will disappear
// when the user goes to enter a real value into the textbox.
//
// usage: $("input").defaultBlur({
// defaultText:'Default text',
// blurClass:'className'
// });
//
// Options:
//
// defaultText: (string, default: "Type here.", required)
// The text to be shown when there is no input value.
//
// blurClass: (string, default: "") The css class that will
// style the default text.
//
//
// Demo: http://jsbin.com/opita
//
// version history:
// v1.1 - added support for password type input fields.
(function($) {
$.fn.defaultBlur = function(options) {
var opts = $.extend({}, $.fn.defaultBlur.defaults, options);
var s = this.selector, c = this.context;
return this.each(function() {
var $this = $(this);
if ($this.val() == "" || $this.val() == opts.defaultText) {
if ($this.attr("type") == "password") {
$(document).data(s + "password", true);
$this.replaceWith("<input type='text' id='" + $this.attr("id") + "' " + getAttrs($this.get(0).attributes) + "/>");
$this = $(s, c);
} else {
$(document).data(s + "password", false);
}
$this.val(opts.defaultText);
$this.addClass(opts.blurClass);
}
$(s, c).live("focusout", function() {
if ($this.val() == "") {
$this.addClass(opts.blurClass);
if ($(document).data(s + "password") == true) {
$this.replaceWith("<input type='text' id='" + $this.attr("id") + "' " + getAttrs($this.get(0).attributes) + "/>");
$this = $(s, c);
$this.blur();
}
$this.val(opts.defaultText);
}
}).live("focusin", function() {
$this.removeClass(opts.blurClass);
if ($this.val() == opts.defaultText) {
if ($(document).data(s + "password") == true) {
$this.replaceWith("<input type='password' id='" + $this.attr("id") + "' " + getAttrs($this.get(0).attributes) + "/>");
$this = $(s, c);
$this.focus();
}
$this.val("");
}
});
$this.closest("form").submit(function() {
if ($this.val() == opts.defaultText) {
$this.val("");
}
return true;
});
});
};
function getAttrs(attrs) {
var attrsToAppend = "";
for (i = 0; i < attrs.length; i++) {
if (attrs[i].nodeName != 'type' || attrs[i].nodeName != 'id') {
if (attrs[i].nodeValue != "" && attrs[i].nodeValue != null) {
attrsToAppend += attrs[i].nodeName + "=" + attrs[i].nodeValue + " ";
}
}
}
return attrsToAppend;
}
$.fn.defaultBlur.defaults = {
defaultText: 'Type here.',
blurClass: ''
};
})(jQuery);