forked from enriquez/ezpz-hint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.ezpz_hint.js
84 lines (72 loc) · 2.92 KB
/
jquery.ezpz_hint.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// EZPZ Hint v1.1.1; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($){
$.fn.ezpz_hint = function(options, focus_callback, blur_callback){
var defaults = {
hintClass: 'ezpz-hint',
hintName: 'ezpz_hint_dummy_input'
};
var settings = $.extend(defaults, options);
return this.each(function(i){
var id = settings.hintName + '_' + i;
var hint;
var dummy_input;
var text;
var ctrl_type;
// grab the input's title attribute
text = $(this).attr('title');
//extract the control type, can't use ctrl.attr('type') because opera returns text for search boxes
var typeRegex = /type="(\w+)"/
var regexResult = typeRegex.exec($(this).attr('outerHTML'))
if (regexResult) //ie6 doens't support regex exec'
{
ctrl_type = regexResult[1]
} else {
ctrl_type = $(this).attr('type');
}
// create a dummy input and place it before the input
$('<input type="' + ctrl_type + '" id="' + id + '" value="" />')
.insertBefore($(this));
// set the dummy input's attributes
hint = $(this).prev('input:first');
hint.attr('class', $(this).attr('class'));
//some browsers don't support the size attrib
var inputsize = $(this).attr('size')
if (typeof(inputsize) == 'number' && inputsize > 0)
{
hint.attr('size',inputsize);
}
else {
hint.attr('width', $(this).css('width'));
}
hint.attr('autocomplete', 'off');
hint.attr('tabIndex', $(this).attr('tabIndex'));
hint.addClass(settings.hintClass);
hint.val(text);
// hide the input
$(this).hide();
// don't allow autocomplete (sorry, no remember password)
$(this).attr('autocomplete', 'off');
// bind focus event on the dummy input to swap with the real input
hint.focus(function(){
dummy_input = $(this);
$(this).next('input:first').show();
$(this).next('input:first').focus();
$(this).next('input:first').unbind('blur').blur(function(){
if ($(this).val() == '') {
$(this).hide();
dummy_input.show();
if (blur_callback)
blur_callback();
}
});
$(this).hide();
if (focus_callback)
focus_callback();
});
// swap if there is a default value
if ($(this).val() != ''){
hint.focus();
}
});
};
})(jQuery);