-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jplaceholder.js
58 lines (51 loc) · 1.53 KB
/
jquery.jplaceholder.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
/**
* jquery input placeholder
* usage: jQuery(".class_name").placeholder();
* usage: jQuery(".class_name").placeholder({'label':'my label...','customClass':'myCustomStyle'});
* author: matteo caberlotto
* contributor: flavio vianello
*/
jQuery.fn.placeholder = function(options) {
// default values
// label : the label to use as placeholder
// customStyle : the class to be added as placeholder style
var defaults = {
label : 'Insert your data here...',
customClass : 'placeholderStyle'
};
// actual options
var opts = $.extend(defaults, options);
this.each(function(){
// cache the current node for performance
var currentNode = jQuery(this);
// bind focus behaviour (placeholder disappear)
currentNode.focus(function(){
if( currentNode.val() == opts.label )
{
currentNode.val('').removeClass(opts.customClass);
}
})
// bind blur behaviour (placeholder comes back)
.blur(function(){
if( currentNode.val() == '' )
{
currentNode.addClass(opts.customClass).val(opts.label);
}
});
// if the form is submitted with the defualt value, clean it
currentNode.parents('form').submit(function(){
if(currentNode.val() == opts.label)
{
currentNode.val('');
}
});
// at beginning, fill input with placeholders
// (do not consider fields already filled, for example on form validation errors)
if( currentNode.val() == '' || currentNode.val() == opts.label )
{
currentNode.val(opts.label).addClass(opts.customClass);
}
});
// allow jquery chaining
return( this );
};