Created
May 4, 2016 16:25
-
-
Save itwasmattgregg/2d080d9540738c820e0701ee79661c3d to your computer and use it in GitHub Desktop.
Select dropdown turned into radio buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select class="radioSelect" id="sizeOptions"> | |
<option value="8x8 (Aluminum)">8x8 (Aluminum)</option> | |
<option value="M">M</option> | |
<option value="L">L</option> | |
</select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
$('.radioSelect').each(function (selectIndex, selectElement) { | |
var select = $(selectElement); | |
var container = $("<div class='radioSelectContainer' />"); | |
select.parent().append(container); | |
container.append(select); | |
select.find('option').each(function (optionIndex, optionElement) { | |
var radioGroup = select.attr('id') + "Group"; | |
var label = $("<label />"); | |
container.append(label); | |
$("<input type='radio' name='" + radioGroup + "' />") | |
.attr("value", $(this).val()) | |
//.click((function () { select.val($(this).val()); })) //radio updates select - see optional below | |
.appendTo(label); | |
$("<span>" + $(this).val() + "</span>").appendTo(label); | |
}); | |
//http://stackoverflow.com/questions/4957207/how-to-check-uncheck-radio-button-on-click | |
//optional - this logic handles unchecking when clicking on an already checked radio | |
container.find(":radio + span").mousedown( | |
function (e) { | |
var $span = $(this); | |
var $radio = $($span.prev()); | |
if ($radio.is(':checked')) { | |
var uncheck = function() { | |
setTimeout(function () { $radio.prop('checked', false); }, 0); | |
}; | |
var unbind = function() { | |
$span.unbind('mouseup', up); | |
}; | |
var up = function() { | |
uncheck(); | |
unbind(); | |
}; | |
$span.bind('mouseup', up); | |
$span.one('mouseout', unbind); | |
} else { | |
select.val($radio.val()); | |
} | |
} | |
); | |
select.change((function () { //select updates radio | |
$("input[name='" + select.attr('id') + "Group" + "'][value='" + this.value + "']").prop("checked", true); | |
})); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.radioSelectContainer > select { | |
display: none; | |
} | |
.radioSelectContainer > label { | |
display: block; | |
width: 50%; | |
margin: 0.3em auto .3em; | |
background-color:#EFEFEF; | |
border-radius:4px; | |
border:1px solid #D0D0D0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment