forked from skorecky/Add-Clear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddclear.js
57 lines (52 loc) · 1.53 KB
/
addclear.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
// Author: Stephen Korecky
// Website: http://stephenkorecky.com
// Plugin Website: http://github.com/skorecky/Add-Clear
(function($){
$.fn.extend({
addClear: function(options) {
var options = $.extend({
closeSymbol: "✖",
color: "#CCC",
top: 1,
right: 4,
returnFocus: true,
showOnLoad: false,
onClear: null
}, options);
$(this).wrap("<span style='position:relative;' class='add-clear-span'>");
$(this).after("<a href='#clear'>"+options.closeSymbol+"</a>");
$("a[href='#clear']").css({
color: options.color,
'text-decoration': 'none',
display: 'none',
'line-height': 1,
overflow: 'hidden',
position: 'absolute',
right: options.right,
top: options.top
}, this);
if($(this).val().length >= 1 && options.showOnLoad === true) {
$(this).siblings("a[href='#clear']").show();
}
$(this).keyup(function() {
if($(this).val().length >= 1) {
$(this).siblings("a[href='#clear']").show();
} else {
$(this).siblings("a[href='#clear']").hide();
}
});
$("a[href='#clear']").click(function(){
$(this).siblings("input").val("");
$(this).hide();
if(options.returnFocus === true){
$(this).siblings("input").focus();
}
if (options.onClear){
options.onClear($(this).siblings("input"));
}
return false;
});
return this;
}
});
})(jQuery);