Bootstrap - drag and drop attribute for multiple buttons

I have a few buttons. When the user clicks on any button, I want to give the impression that the button is pressed.

This is the code:

<table>
<thead>
<tr>
<th><button class="btn btn-danger" >Today</button></th>
<th><button class="btn btn-danger" >Tomorrow</button></th>
<th><button class="btn btn-danger" >DayAfterTomorrow</button></th>
</tr>
</thead>
</table>

And this works when I have one button:

<button class="btn" data-toggle="button">Today</button>

If the user clicks tomorrow, the data switch attribute still remains for the Today button. Instead, I want to disable the drag and drop attribute when another button is clicked.

+5
source share
5 answers

You can use the bootstrap switch functionality to achieve this effect. Try the following:

<div data-toggle="buttons-radio">
    <button class="btn btn-danger">Today</button>
    <button class="btn btn-danger">Tomorrow</button>
    <button class="btn btn-danger">DayAfterTomorrow</button>
</div>

Demo: http://jsfiddle.net/u7Lg8/

+5
source

Try it like this:

$('.btn').click(function(){
    //Removing `data-toggle` from all elements
    $('.btn').removeData('toggle');
    //Adding `data-toggle` on clicked element
    $(this).data('toggle','button');        
});
+1

div .

Twitter-

<!-- Add data-toggle="buttons-checkbox" for checkbox style toggling on btn-group -->
<div class="btn-group" data-toggle="buttons-checkbox">
  <button class="btn">Left</button>
  <button class="btn">Middle</button>
  <button class="btn">Right</button>
</div>
0

Here is what I came up with for my site (only way to improve - DO NOT use a hidden variable to store the final value)

     $("input[name='dispatch']").change(function(){
           
            $(this).parent().siblings().removeClass('active btn-primary');
            $("#WantsDispatchService").val($(this).val());
            
        });
 
  <div class="btn-group btn-toggle  " data-toggle="buttons">
	<label class="btn btn-default btn-sm">
		<input name="dispatch" value="false" type="checkbox"> No
	</label>
	<label class="btn btn-default  btn-sm">

		<input name="dispatch" value="true" type="checkbox"> Yes
	</label>
	 
</div>
<input type ="hidden" id="WantsDispatchService" name="WantsDispatchService" value="false"> 
</div>
Run codeHide result
0
source

You can use $().button('toggle')buttons to switch the state using javascript. *

In my case; I cannot use grouped buttons, they are visually separated from each other.

I check if another button is clicked and toggles using javascript. Here is my solution:

$('#buttonA').click(function (e) {
    if ($('#buttonB').attr('aria-pressed') == 'true') {
        $('#buttonB').button('toggle');
    }
    e.preventDefault();
});

$('#buttonB').click(function (e) {
    if ($('#buttonA').attr('aria-pressed') == 'true') {
        $('#buttonA').button('toggle');
    }
    e.preventDefault();
});
0
source

All Articles