-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptionTile.js
52 lines (33 loc) · 1.26 KB
/
optionTile.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
// optionTile
// Transforms marked select inputs into option tile menus
// using them as data sources.
// Steve Knoblock 2015
// MIT License
$(document).ready(function(){
// iterate over source selects
$('select.option-tile-source').each(function(ind, val){
var optionTiles = '';
console.log(this); // this contains the selected element and all of its children
optionTiles += '<div class="option-surface">';
optionTiles += ' <ul id="' + $( this ).attr( 'id' ) + '" class="option-tiles">';
$.each(this, function(index, value) {
optionTiles += ' <li class="option-tile" data-select-index="' + $( this ).attr( 'value' ) + '" data-cost="' + $(this).attr('data-cost') + '"> <span>' + $( this ).text() + '</span> <span class="delta"></span></li>';
});
optionTiles += '</ul></div>';
$("body").append(optionTiles);
});
$('.option-tiles li').each(function(index) {
$(this).on('click', function() {
var data = $(this).attr('data-select-index');
var delta = $(this).attr('data-cost');
var id = $(this).parent().attr('id');
$('#' + id).val(data);
// If a cost or callout is defined for this option
// display it
if( delta !== 'undefined' ) {
$('span.delta').text('');
$(this).children('span.delta').text(delta);
}
});
});
});