forked from marcgg/Simple-Placeholder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleplaceholder.js
69 lines (60 loc) · 2.05 KB
/
jquery.simpleplaceholder.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
/*
* Simple Placeholder by @marcgg under MIT License
* Report bugs or contribute on Gihub: https://github.com/marcgg/Simple-Placeholder
*/
(function($) {
$.simplePlaceholder = {
placeholderClass: null,
hidePlaceholder: function(){
var $this = $(this);
if($this.val() == $this.attr('placeholder') && $this.data($.simplePlaceholder.placeholderData)){
$this
.val("")
.removeClass($.simplePlaceholder.placeholderClass)
.data($.simplePlaceholder.placeholderData, false);
}
},
showPlaceholder: function(){
var $this = $(this);
if($this.val() == ""){
$this
.val($this.attr('placeholder'))
.addClass($.simplePlaceholder.placeholderClass)
.data($.simplePlaceholder.placeholderData, true);
}
},
preventPlaceholderSubmit: function(){
$(this).find(".simple-placeholder").each(function(e){
var $this = $(this);
if($this.val() == $this.attr('placeholder') && $this.data($.simplePlaceholder.placeholderData)){
$this.val('');
}
});
return true;
}
};
$.fn.simplePlaceholder = function(options) {
if(document.createElement('input').placeholder == undefined){
var config = {
placeholderClass : 'placeholding',
placeholderData : 'simplePlaceholder.placeholding'
};
if(options) $.extend(config, options);
$.extend($.simplePlaceholder, config);
this.each(function() {
var $this = $(this);
$this.focus($.simplePlaceholder.hidePlaceholder);
$this.blur($.simplePlaceholder.showPlaceholder);
$this.data($.simplePlaceholder.placeholderData, false);
if($this.val() == '') {
$this.val($this.attr("placeholder"));
$this.addClass($.simplePlaceholder.placeholderClass);
$this.data($.simplePlaceholder.placeholderData, true);
}
$this.addClass("simple-placeholder");
$(this.form).submit($.simplePlaceholder.preventPlaceholderSubmit);
});
}
return this;
};
})(jQuery);