-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hint.js
111 lines (90 loc) · 3.97 KB
/
jquery.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
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
/*
* jQuery hint plugin
*
* Copyright (c) 2012 Mike Fitzgerald
* http://www.xpressivecode.com
*
Allows you to generate hints for your textboxes without modifying the value of your textboxes.
This helps to avoid the situation of validating your form submissions against the hint values,
when a user blindly submits a form. The textboxes value will be empty as this solution uses a
span overlay instead of modifying the value of your textboxes.
Usage:
*assumes you have at least 1 input field of type "text" with a title attribute filled out
*example: <input type="text" title="Search..." name="search" />
<script type="text/javascript">
$(document).ready(function(){
$.hint();
});
</script>
You can pass in some options via anonymous object
className:
default value: 'hint'
use: applied to the span so that you can target it for styling
so that you can give it an italic look if you want etc
color:
default value: '#B3AAAD'
use: gives a little bit of an offset colour to look like a hint
element:
default value: 'span'
use: allows you to change the element type that is used to overlay your textbox
cloneStyles:
default value: true
use: automatically clones css attributes from the textbox and applies them to your
hint overlay. this helps to rectiy positioning issues with padding and margins
styles:
default value: ['font-size', 'font-family', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left']
use: these are the css attributes that will be cloned from the textbox to your overlay element if the cloneStyles option is set to true
skipStyles:
defalut value: [],
use: if you want to keep the majority of the styles to clone them, but want to omit just a few, then use this setting to skip them
example: if you want to omit the padding but retain the rest of your textboxes css attributes then you could pass in
$.hint({ skipStyles: ['padding-top', 'padding-right', 'padding-bottom', 'padding-left'] });
*/
(function($){
if($.hint)return;
$.hint = function(options){
var defaults = {
className: 'hint',
cloneStyles: true,
styles: ['font-size', 'font-family', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left'],
skipStyles: [],
color: '#B3AAAD',
element: 'span'
}
var settings = $.extend({}, defaults, options);
$(':input[title]').each(function(){
var txt = $(this);
var title = txt.attr('title');
var lbl = $('<' + settings.element + '>');
lbl.text(title);
if(txt.val() != ''){
lbl.hide();
}
lbl.css('color', settings.color);
lbl.addClass(settings.className);
lbl.css('position', 'absolute');
lbl.css('top', txt.offset().top);
lbl.css('left', txt.offset().left);
if(settings.cloneStyles){
$.each(settings.styles, function(index, style){
if($.inArray(style, settings.skipStyles) === -1){
lbl.css(style, txt.css(style));
}
});
}
lbl.click(function(){
lbl.hide();
txt.focus();
});
txt.focus(function(){
lbl.hide();
});
txt.blur(function(){
if(txt.val() === title || txt.val() === ''){
lbl.show();
}
});
$(document.body).append(lbl);
});
};
})(jQuery);