/
example.html
289 lines (256 loc) · 7.23 KB
/
example.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Custom Radio buttons - Mario Iliev</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<style>
/* Presentation CSS */
body {
margin: 0;
padding: 0;
font-family: 'Open Sans',Helvetica,Arial,sans-serif;
height: 600px;
}
.wrap {
opacity: 0;
max-width: 500px;
padding-bottom: 30px;
margin: 50px auto 0 auto;
}
.heading {
color: #009481;
text-align: center;
margin: 0 0 40px 0;
}
.heading a {
color: #FFA11B;
}
.explain {
margin: 0 0 40px 0;
padding: 30px 0 0 0;
border-top: 1px solid #ccc;
text-align: center;
}
.explain span {
display: block;
width: 100%;
padding: 7px 15px;
margin: 8px 0 0 0;
font-size: 15px;
color: #394873;
background-color: #ECECEC;
box-sizing: border-box;
}
.explain:first-child {
border: none;
}
.destroy {
display: table;
color: #424242;
padding: 5px 20px;
margin: 10px auto 0 auto;
border-radius: 5px;
border: 1px solid #424242;
text-decoration: none;
}
.destroy:hover {
color: #FFF;
background-color: #000;
}
div:focus {
outline: none;
}
.ui-loader {
display: none;
}
/* Plugin CSS */
form {
display: table;
min-height: 42px;
margin: 0 auto 40px auto;
}
label {
float: left;
width: 100px;
background-color: #DEDEDE;
border-radius: 4px;
padding: 10px;
margin-right: 20px;
text-align: center;
}
label.selected {
color: #fff;
background-color: #17bed0;
}
</style>
</head>
<body>
<div class="wrap">
<div class="heading">
<h1>jQuery Custom Radio buttons by Mario Iliev</h1>
Download from Github: <a href="https://github.com/mario-iliev/custom-radio" target="_blank">Download</a>
<h3>Some examples:</h3>
</div>
<div class="explain">
Called without parameters: <b><div data-radio-custom>...</div></b>
</div>
<form>
<div data-radio-custom>
<input type="radio" name="material" value="Steel">
<input type="radio" name="material" value="Silver">
<input type="radio" name="material" value="Gold">
</div>
</form>
<div class="explain">
Called manually with <b>custom</b> text. <br>Original input values (<b>Steel</b>, <b>Silver</b>, <b>Gold</b>) are kept.<br>
<span>$('.custom-radio1').customRadio({text: 'data-custom-title'});</span>
</div>
<form>
<div class="custom-radio1">
<input type="radio" name="material" value="Steel" data-custom-title="Metal Coin">
<input type="radio" name="material" value="Silver" data-custom-title="Silver Coin">
<input type="radio" name="material" value="Gold" data-custom-title="Golded Coin">
</div>
</form>
<div class="explain">
Click to <b>destroy</b> the plugin<br>
<a href="javascript:;" class="destroy">Destroy</a>
</div>
</div>
<script>
/*
Project: Custom Radio buttons
Description: jQuery plugin to replace radio inputs
Author: Mario Iliev
---------------------------
Initialize the plugin (two options):
1. Call the plugin on element containing radio buttons - $(selector).customRadio();
2. Add "data-radio-custom" tag on <div> containing radio buttons and the plugin will run with the default options
---------------------------
Radio button text:
If no additional paramters are specified, the plugin will take the radio input value text
Available options for "text:" are: 'empty', 'value', 'title' or custom 'data' type value.
Example: $(selector).customRadio({text: 'data-custom-title'});
---------------------------
Disable radio buttons:
$(selector).customRadio({disabled: true});
- the <label> wrapper gets "disabled" class selector
---------------------------
To destroy the plugin:
$(selector).customRadio({destroy: true});
---------------------------
Styling:
Each radio button is wrapped in <label> element with the following class selectors:
"radio_btn"
"selected" (when button is clicked)
"radio_#" ("#" is uniqe number for each radio button)
"customClass:" You can add custom class selector:
- $(selector).customRadio({customClass: 'myclass'});
Each Label contains <span> with the text of the radio button
*/
;(function ($) {
'use strict';
var instance = 0;
var beenDisabled = 0;
var RadioButtons = function (el, opts) {
var element = $(el).is('[type=radio]') ? $(el) : $(el).find('[type=radio]');
var alreadyCalled = element.parent().is('label');
var customClass = opts.customClass ? ' ' + opts.customClass : '';
var disabled = opts.disabled;
var textSource = opts.text;
var destroy = opts.destroy;
var text = function(el) {
var textValue = $(el).attr(textSource);
if (textSource == 'empty') return '';
else if (!textValue) return 'No text source';
else if (textSource) return textValue;
};
var clicкEvents = function(labels, thisLabel) {
labels.parent().removeClass('selected');
$(thisLabel).addClass('selected');
};
var destroyPlugin = function(element) {
if (element.parent().is('label')) {
element
.removeAttr('style')
.parent()
.off('touchstart.customradio click.customradio')
.find('span').remove();
element.unwrap();
}
};
// Hide input radio
if (destroy) {
destroyPlugin(element);
return;
} else {
element.css({'position':'absolute', 'top':'0', 'left':'0', 'margin':'0', 'z-index':'-1', 'opacity':'0'});
}
// We only need to do this only once
if (!alreadyCalled) {
element
.wrap('<label/>')
.parent()
.append('<span>')
.on('touchstart.customradio click.customradio', function() {
clicкEvents(element, this);
})
.css({'cursor':'pointer', 'position':'relative'});
}
// Flexible changes if "customRadio" is called more than once
element.each(function() {
var object_number = ++instance;
$(this)
.parent()
.removeAttr('class')
.addClass('radio_btn radio_' + object_number + customClass)
.find('span').text(text(this));
});
// If we want to disable radio buttons
if (disabled) {
beenDisabled++;
element
.attr('disabled', true)
.parent()
.addClass('disabled')
.css({'cursor':'not-allowed'})
.off('touchstart.customradio click.customradio');
} else {
element
.attr('disabled', false)
.parent()
.removeClass('disabled')
.css({'cursor':'pointer'});
}
// Return click events if we re-enable buttons
if (alreadyCalled && beenDisabled != 0) {
element
.parent()
.on('touchstart.customradio click.customradio', function() {
clicкEvents(element, this);
});
}
};
$.fn.customRadio = function (options, callback) {
var opts = $.extend({}, $.fn.customRadio.defaults, options, callback);
return this.each(function () {
new RadioButtons($(this), opts);
});
}
$.fn.customRadio.defaults = { text: 'value', customClass: '', disabled: false, destroy: false };
$('div[data-radio-custom]').customRadio();
})(jQuery);
// Manual plugin call
$(function() {
$('.wrap').animate({'opacity' : 1});
$('.custom-radio1').customRadio({text: 'data-custom-title'});
$('.destroy').on('click.destroy', function() {
$('body').customRadio({destroy: true});
$('.destroy').off('click.destroy').text('Destroyed').css({'cursor':'default'});
});
});
</script>
</body>
</html>