This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.toggleval.js
104 lines (93 loc) · 3.41 KB
/
jquery.toggleval.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
/* -------------------------------------------------- *
* ToggleVal 3.0
* Updated: 01/15/2010
* -------------------------------------------------- *
* Author: Aaron Kuzemchak
* URL: http://aaronkuzemchak.com/
* Copyright: 2008-2010 Aaron Kuzemchak
* License: MIT License
** -------------------------------------------------- */
(function($) {
// main plugin function
$.fn.toggleVal = function(theOptions) {
// check whether we want real options, or to destroy functionality
if(!theOptions || typeof theOptions == 'object') {
theOptions = $.extend({}, $.fn.toggleVal.defaults, theOptions);
}
else if(typeof theOptions == 'string' && theOptions.toLowerCase() == 'destroy') {
var destroy = true;
}
return this.each(function() {
// unbind everything if we're destroying, and stop executing the script
if(destroy) {
$(this).unbind('focus.toggleval').unbind('blur.toggleval').removeData('defText');
return false;
}
// define our variables
var defText = '';
// let's populate the field, if not default
switch(theOptions.populateFrom) {
case 'title':
if($(this).attr('title')) {
defText = $(this).attr('title');
$(this).val(defText);
}
break;
case 'label':
if($(this).attr('id')) {
defText = $('label[for="' + $(this).attr('id') + '"]').text();
$(this).val(defText);
}
break;
case 'custom':
defText = theOptions.text;
$(this).val(defText);
break;
default:
defText = $(this).val();
}
// let's give this field a special class, so we can identify it later
// also, we'll give it a data attribute, which will help jQuery remember what the default value is
$(this).addClass('toggleval').data('defText', defText);
// now that fields are populated, let's remove the labels if applicable
if(theOptions.removeLabels == true && $(this).attr('id')) {
$('label[for="' + $(this).attr('id') + '"]').remove();
}
// on to the good stuff... the focus and blur actions
$(this).bind('focus.toggleval', function() {
if($(this).val() == $(this).data('defText')) { $(this).val(''); }
// add the focusClass, remove changedClass
$(this).addClass(theOptions.focusClass);
}).bind('blur.toggleval', function() {
if($(this).val() == '' && !theOptions.sticky) { $(this).val($(this).data('defText')); }
// remove focusClass, add changedClass if, well, different
$(this).removeClass(theOptions.focusClass);
if($(this).val() != '' && $(this).val() != $(this).data('defText')) { $(this).addClass(theOptions.changedClass); }
else { $(this).removeClass(theOptions.changedClass); }
});
});
};
// default options
$.fn.toggleVal.defaults = {
focusClass: 'tv-focused', // class during focus
changedClass: 'tv-changed', // class after focus
populateFrom: 'default', // choose from: default, label, custom, or title
text: null, // text to use in conjunction with populateFrom: custom
removeLabels: false, // remove labels associated with the fields
sticky: false // if true, default text won't reappear
};
// create custom selectors
// :toggleval for affected elements
// :changed for changed elements
$.extend($.expr[':'], {
toggleval: function(elem) {
return $(elem).data('defText') || false;
},
changed: function(elem) {
if($(elem).data('defText') && $(elem).val() != $(elem).data('defText')) {
return true;
}
return false;
}
});
})(jQuery);