"Sexy Radio Butons"
Bootstrap 3.1.0 Snippet by tonnevillec

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <h2>Use butons to simulate radio butons</h2> <div class="form-group"> <label for="happy" class="col-sm-4 col-md-4 control-label text-right">Are you happy ?</label> <div class="col-sm-7 col-md-7"> <div class="input-group"> <div id="radioBtn" class="btn-group"> <a class="btn btn-primary btn-sm active" data-toggle="happy" data-title="Y">YES</a> <a class="btn btn-primary btn-sm notActive" data-toggle="happy" data-title="N">NO</a> </div> <input type="hidden" name="happy" id="happy"> </div> </div> </div> <br /><br /> <div class="form-group"> <label for="fun" class="col-sm-4 col-md-4 control-label text-right">Is it fun ?</label> <div class="col-sm-7 col-md-7"> <div class="input-group"> <div id="radioBtn" class="btn-group"> <a class="btn btn-primary btn-sm active" data-toggle="fun" data-title="Y">YES</a> <a class="btn btn-primary btn-sm notActive" data-toggle="fun" data-title="X">I don't know</a> <a class="btn btn-primary btn-sm notActive" data-toggle="fun" data-title="N">NO</a> </div> <input type="hidden" name="fun" id="fun"> </div> </div> </div> </div> <br /><br /> <div class="row"> <div class="panel panel-info"> <div class="panel-heading"><h2 class="panel-title">How to use</h2></div> <div class="panel-body"> You can use this snippet with a normal form and get the returns value using the hidden input. </div> </div> </div> </div>
#radioBtn .notActive{ color: #3276b1; background-color: #fff; }
$('#radioBtn a').on('click', function(){ var sel = $(this).data('title'); var tog = $(this).data('toggle'); $('#'+tog).prop('value', sel); $('a[data-toggle="'+tog+'"]').not('[data-title="'+sel+'"]').removeClass('active').addClass('notActive'); $('a[data-toggle="'+tog+'"][data-title="'+sel+'"]').removeClass('notActive').addClass('active'); })

Related: See More


Questions / Comments:

if submitting as a form, should add a line of code to end of the js function $('input[name="happy"]').val($(this).data('title'));

Aaron Elbaz () - 8 years ago - Reply 0


Doesn't work in tabs. Even on this site, switch to HTML, CSS or JS and when you switch back to the Preview, it won't work.

Dezso Markon () - 8 years ago - Reply 0


@aven_parker:disqus The value will be set by this line of js code: $('#'+tog).prop('value', sel);

Chris () - 8 years ago - Reply 0


please tell me how i'm suppose to fetch results from the hidden input because there's no value attribute..?

Aven Parker () - 9 years ago - Reply 0


very cool. good job! thank you for sharing

simo () - 10 years ago - Reply 0