forked from phallguy/jquery-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.placify.js
63 lines (56 loc) · 1.8 KB
/
jquery.placify.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
/*
* Placify
* Copyright 2011 Apps In Your Pants Corporation
* http://github.com/appsinyourpants/jquery-plugins
*
* Add support for input HTML5 placeholder attribute for legacy and modern browsers.
*
* Version 1.0 - Updated: Jan. 20, 2011
*
* This jQuery plug-in is lcensed under a Creative Commons Attribution 3.0 Unported License. http://creativecommons.org/licenses/by/3.0/
*/
(function($) {
$.fn.placify = function() {
var defaults = {
cssClass: 'placeholder',
containerType: 'div',
fadeSpeed: 200
};
var opts = $.extend(defaults, arguments[0] || {});
this.each(function() {
if (this.placify) return;
this.placify = true;
var input = $(this);
// If not an input element, then placify all child inputs with a placeholder attribute.
if( !input.is('input') )
{
$('input[placeholder]',input).placify(opts);
return;
}
var container = $('<' + opts.containerType + ' class="' + opts.cssClass + '" />')
.css({
position: 'absolute',
display: input.val().length ? 'none' : 'block',
left: input.position().left,
top: input.position().top
})
.html('<label for="' + input.attr('id') + '">' + input.attr('placeholder') + '</label>');
input.before(container)
.removeAttr('placeholder') // Remove so default browser rendering is hidden
.focus(function(){
container.fadeOut(opts.fadeSpeed);
})
.blur(function(){
if( input.val().length == 0 )
container.fadeIn(opts.fadeSpeed);
})
.change(function(){
if( input.val().length > 0 )
container.fadeOut(opts.fadeSpeed);
else if( input.val().length == 0 )
container.fadeIn(opts.fadeSpeed);
});
});
return this;
}
})(jQuery);