[ACCEPTED]-Twitter Bootstrap onclick event on buttons-radio-jquery-events
This is a really annoying one. What I ended 6 up using is this:
First, create a group of 5 simple buttons with no data-toggle
attribute.
<div id="selector" class="btn-group">
<button type="button" class="btn active">Day</button>
<button type="button" class="btn">Week</button>
<button type="button" class="btn">Month</button>
<button type="button" class="btn">Year</button>
</div>
Next, write 4 an event handler that simulates the radio 3 button effect by 'activating' the clicked 2 one and 'deactivating' all other buttons. (EDIT: Integrated 1 Nick's cleaner version from the comments.)
$('#selector button').click(function() {
$(this).addClass('active').siblings().removeClass('active');
// TODO: insert whatever you want to do with $(this) here
});
I see a lot of complicated answers, while 4 this is super simple in Bootstrap 3:
Step 3 1: Use the official example code to create your radio button group, and 2 give the container an id:
<div id="myButtons" class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
</label>
</div>
Step 2: Use this 1 jQuery handler:
$("#myButtons :input").change(function() {
console.log(this); // points to the clicked input button
});
I would use a change event not a click like 1 this:
$('input[name="name-of-radio-group"]').change( function() {
alert($(this).val())
})
For Bootstrap 3 the default radio/button-group structure 2 is :
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="options" id="option1"> Option 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2"> Option 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3"> Option 3
</label>
</div>
And you can select the active one like 1 this:
$('.btn-primary').on('click', function(){
alert($(this).find('input').attr('id'));
});
Don't use data-toggle attribute so that 6 you can control the toggle behavior by yourself. So 5 it will avoid 'race-condition'
my codes:
button 4 group template (written in .erb, embedded 3 ruby for ruby on rails):
<div class="btn-group" id="featuresFilter">
<% _.each(features, function(feature) { %> <button class="btn btn-primary" data="<%= feature %>"><%= feature %></button> <% }); %>
</div>
and javascript:
onChangeFeatures = function(e){
var el=e.target;
$(el).button('toggle');
var features=el.parentElement;
var activeFeatures=$(features).find(".active");
console.log(activeFeatures);
}
onChangeFeatures 2 function will be triggered once the button 1 is clicked.
I needed to do the same thing for a chart 5 where you could select the period of the 4 data that should be displayed.
Therefore 3 I introduced the CSS class 'btn-group-radio' and 2 used the following unobtrusive javascript 1 one-liner:
// application.js
$(document).ready(function() {
$('.btn-group-radio .btn').click(function() {
$(this).addClass('active').siblings('.btn').removeClass('active');
});
});
And here is the HTML:
<!-- some arbitrary view -->
<div class="btn-group btn-group-radio">
<%= link_to '1W', charts_path('1W'), class: 'btn btn-default active', remote: true %>
<%= link_to '1M', charts_path('1M'), class: 'btn btn-default', remote: true %>
<%= link_to '3M', charts_path('3M'), class: 'btn btn-default', remote: true %>
<%= link_to '6M', charts_path('6M'), class: 'btn btn-default', remote: true %>
<%= link_to '1Y', charts_path('1Y'), class: 'btn btn-default', remote: true %>
<%= link_to 'All', charts_path('all'), class: 'btn btn-default', remote: true %>
</div>
Looking at the example HTML for radio buttons 7 on the Twitter Bootstrap page (http://twitter.github.com/bootstrap/base-css.html#forms), you can 6 see that each input has a unique ID attribute, i.e. optionsRadios1
and 5 optionsRadios2
.
The relevant HTML example snippet is included 4 here for completeness:
<div class="controls"> <label class="radio"> <input type="radio" checked="" value="option1" id="optionsRadios1" name="optionsRadios"> Option one is this and that—be sure to include why it's great </label> <label class="radio"> <input type="radio" value="option2" id="optionsRadios2" name="optionsRadios"> Option two can is something else and selecting it will deselect option one </label> </div>
So you can use a jQuery 3 click event, and then use the this
reference 2 to look at the id of the HTML element that 1 was clicked.
$('.controls').find('input').bind('click',function(event){ if($(this).attr('id')==='optionsRadios1'){ alert($(this).attr('id')); } else { //... call some other function } });
If your html is similar to the example, so 2 the click event is produced over the label, not 1 in the input, so I use the next code: Html example:
<div id="myButtons" class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
</label>
</div>
Javascript code for the event:
$('#option1').parent().on("click", function () {
alert("click fired");
});
I searched so many pages: I found a beautiful 1 solution. Check it out:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
$(function() {
$("#my_launch_today_chk").change(function() {
var chk = $(this).prop('checked');
if(chk == true){
console.log("On");
}else{
console.log("OFF");
}
});
});
</script>
</head>
<body >
<input type="checkbox" id="my_launch_today_chk" checked data-on="Launch" data-off="OFF" data-toggle="toggle" data-size="small">
</body>
</html>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.