I was wondering what is the best way to convert select menu into buttons with an initial selected state.
I searched for this solution on Stackoverflow and this is the only link I found close enough. However, there's an error in the code making all items initially selected, when only one item with the "selected" attribute should be highlighted. Original Stackoverflow Post
$(function() {
$("select option").unwrap().each(function() {
var btn = $('<div class="btn">' + $(this).text() + '</div>');
if ($(this).is(':checked')) btn.addClass('on');
$(this).replaceWith(btn);
});
$(document).on('click', '.btn', function() {
$('.btn').removeClass('on');
$(this).addClass('on');
});
});
div.btn {
display: inline-block;
/** other styles **/
}
div.btn.on {
background-color: #777;
color: white;
/** styles as needed for on state **/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="text">
<option selected>Yes</option>
<option>No</option>
</select>