"Sexy Radio Butons #2"
Bootstrap 3.3.0 Snippet by jarodxxx

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
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.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 class="radioBtn btn-group">
<a class="btn btn-primary active" data-toggle="happy" data-title="Y">YES</a>
<a class="btn btn-primary notActive" data-toggle="happy" data-title="N">NO</a>
</div>
<input type="hidden" name="happy" class="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 class="radioBtn btn-group">
<a class="btn btn-primary active" data-toggle="fun" data-title="Y">YES</a>
<a class="btn btn-primary notActive" data-toggle="fun" data-title="X">I don't know</a>
<a class="btn btn-primary notActive" data-toggle="fun" data-title="N">NO</a>
</div>
<input type="hidden" name="fun" class="fun">
</div>
</div>
</div>
</div>
<br /><br />
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
.radioBtn .notActive{
color: #3276b1;
background-color: #fff;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
$(document).ready(function() {
$('.container').on('click', '.radioBtn a', function() {
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$(this).parent().next('.' + tog).prop('value', sel);
$(this).parent().find('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('notActive');
$(this).parent().find('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('notActive').addClass('active');
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments:

correction
$('input[name="' + tog + '"]').val($(this).data('title'));

Aaron Elbaz () - 8 years ago - Reply 0


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