forked from chrisdavies/jquery.placeholderlabels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.placeholderlabels.js
44 lines (37 loc) · 1.5 KB
/
jquery.placeholderlabels.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
/*
* Persistent Placeholder - treats a label like a placeholder and
* makes it persist even when you focus on an input. Huzzah!
*/
(function ($) {
var parentSelector = 'div.input-wrapper';
function update(force) {
var $input = $(this),
$parent = $input.parent(parentSelector);
return $parent[force === true || $input.val() ? 'addClass' : 'removeClass']('filled');
}
function focus() {
update.call(this).addClass('focus');
}
function blur() {
update.call(this).removeClass('focus');
}
var i = 0;
function keydown(evt) {
var c = evt.keyCode;
((47 < c && c < 91) || (95 < c && c < 112) || (185 < c && c < 223)) && update.call(this, true);
}
$.fn.placeholderlabels = function () {
return this.each(update);
};
$(function () {
// only simulate placeholders if they are not supported:
if (document.createElement("input").placeholder === undefined) {
$('input[placeholder], textarea[placeholder]').each(function () {
var me = $(this);
var lbl = $('<label></label>').text(me.attr('placeholder')).click(function () { me.focus(); });
me.removeAttr('placeholder').wrap('<div class="input-wrapper" />').parent().prepend(lbl);
});
$('input, textarea', parentSelector).on('focus', focus).on('blur', blur).on('keyup', update).on('click', update).on('keydown', keydown).placeholderlabels();
}
});
})(jQuery);