forked from brandonaaron/jquery-overlabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.overlabel.js
41 lines (37 loc) · 1.25 KB
/
jquery.overlabel.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
/*! Copyright (c) 2013 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*
* Version 1.2.0
*
* Based on Making Compact Forms More Accessible by Mike Brittain (http://alistapart.com/articles/makingcompactformsmoreaccessible)
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.overlabel = function() {
this.each(function() {
var $label = $(this),
$input = $('#' + $label.attr('for'));
// support calling directly on inputs
if ($label.is('input')) {
$input = $label;
$label = $('label[for='+$input.attr('id')+']', this.form);
}
$label
.addClass('overlabel')
.bind('click', function(event) {
$input.focus();
});
$input
.bind('focus blur', function(event) {
$label.css('display', (event.type == 'blur' && !$input.val() ? '' : 'none'));
}).trigger('blur');
});
};
}));