This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholder.js
78 lines (65 loc) · 2.67 KB
/
placeholder.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
/**
* jQuery placeholder attribute plugin.
*
* Adds support via jQuery for the placeholder attribute in text inputs
* and text areas for browsers which do not support the feature.
*
* Copyright (c) 2011 Blake Gardner, http://blakerg.wordpress.com/
* Release under the MIT License. See License.txt
*
*/
(function( $ ) {
$.fn.Placeholder = function(options) {
// detect if we are running IE9 or greater
if($.browser.msie && parseInt($.browser.version, 10) >= 9) {
detect_pass = true;
} else {
detect_pass = false;
}
var selector = 'input[type="text"][placeholder!=""], input[type="password"][placeholder!=""], textarea[placeholder!=""]';
// bind the form to clear form fields
$("form", this).bind('submit', function() {
$(selector, this).each(function() {
if($(this).val() == $(this).attr('placeholder')) {
$(this).val("");
}
});
});
// loop over all inputs
$(selector, this).each(function() {
if($(this).val() == "" || $(this).val() == $(this).attr('placeholder')) {
// set the placeholder values
$(this).val($(this).attr('placeholder'));
$(this).addClass(options.defaultClass);
// convert a password input to a text input
if(detect_pass && $(this).attr('type') == 'password') {
$(this).addClass('was_pw');
this.type = "text";
}
}
$(this).bind({
focus: function() {
if($(this).val() == $(this).attr('placeholder')) {
$(this).val("");
$(this).removeClass(options.defaultClass);
}
if(detect_pass && $(this).hasClass('was_pw')) {
this.type = "password";
}
if($(this).val() == "") {
$(this).select();
}
},
blur: function() {
if($(this).val() == "") {
$(this).addClass(options.defaultClass);
$(this).val($(this).attr('placeholder'));
if(detect_pass && $(this).hasClass('was_pw')) {
this.type = "text";
}
}
}
}); // bind
}); // end each loop
};
})(jQuery);