-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.help-text.js
138 lines (123 loc) · 4.47 KB
/
jquery.help-text.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* A small plugin that adds a helpText to an input field.
* You can also modify the ENTER key behaviour.
*
* Example:
*-------------------------------------------------
$(document).ready(function() {
$('#username').helpText({
text: 'email@host.com',
enter: 'tab',
focus: 1
});
$('#password').helpText({
text: 'password'
});
});
*
* @author deterset (Robert Campbell)
* @homepage http://deterset.com
* @created 2014-07-14
* @version 0.3 - Tweek the init() function to setup each input field with the
* dimmed help text. Added jquery.help-text.css to default the
* dimmed help text class. init can also set the focus if
* requested.
* @version 0.2 - initial working realease
*/
(function($) {
$.fn.helpText = function(options) {
// merges the options w/defaults
var settings = $.extend({
helpClass: 'helptext_dimmed',
text: '',
enter: 'submit', // option 'submit'(default browser behaviour)
// or 'tab' to next input field
focus: 0 // 1 after page load focus this input
}, options);
this.init = function () {
$(this).each(function() {
if($(this).val() === '') {
// adds the helpText back in
$(this).val(settings.text);
// adds the helpClass
$(this).addClass(settings.helpClass);
}
$(this).attr("helpText",settings.text);
if (settings.focus) {
this.focus();
this[0].setSelectionRange(0, 0);
settings.focus = 0;
}
});
};
// clean off input help text before submitting form.
function remove() {
$("input").each(function() {
if ($(this).val() === $(this).attr('helpText')) {
$(this).val('');
settings.text = '';
}
});
}
this.closest("form").addClass('helpTextFormClass');
$(document).on('submit', 'form.helpTextFormClass', function(e) {
remove();
});
// Prevent Form from being submitted on enter
this.closest("form").keypress(function(e) {
return e.keyCode != 13;
});
// before the 1st users character is output remove help text.
this.keypress(function(e) {
if(e.keyCode != 13 && $(this).val() == $(this).attr("helpText")) {
// resets the value in the field
$(this).val("");
}
if ($(this).val() == '') {
// removes the helpClass
$(this).removeClass(settings.helpClass);
}
return e.keyCode != 13;
});
// modified enter key behaviour
this.keyup(function(e) {
if ((e.keyCode == 13 && settings.enter == 'tab')) {
// :input below is necessary for IE to work
var tabables = $("[tabindex != '-1']:input:visible");
var index = tabables.index(this);
tabables.eq(index + 1).focus();
} else if (e.keyCode == 13 && settings.enter == 'submit') {
remove();
$(this).closest("form").submit();
}
});
// when the field receives focus update cursor position
// helpText pos = begining, userText pos = end of string
this.focus(function() {
if($(this).val() == $(this).attr("helpText")) {
// resets the value in the field
$(this)[0].setSelectionRange(0, 0);
} else {
if ($(this)[0].setSelectionRange) {
var len = $(this).val().length;
$(this)[0].setSelectionRange(len,len);
} else {
$(this).val($(this).val());
}
}
});
// when the user leaves the field return helpText if empty
this.blur(function() {
if($(this).val() === '') {
// adds the helpText back in
$(this).val(settings.text);
// adds the helpClass
$(this).addClass(settings.helpClass);
}
});
// Initialize the plugin instance
this.init();
// allows for jQuery chaining
return $(this);
};
}(jQuery));