I have the following markup for a group of buttons:
<div class="btn-group pull-right">
<button type="button" class="btn btn-default" id="today">Today</button>
<button type="button" class="btn btn-default" id="this_week">This Week</button>
<button type="button" class="btn btn-default" id="this_month">This Month</button>
<button type="button" class="btn btn-default" id="custom">Custom</button>
</div>
I'm trying to replace a class btn-defaultwith a class btn-primaryon a button using jQuery so that when I click on the button I clicked the next one class="btn btn-primary"and any other button in the group with the class will btn-primaryswitch to btn-default.
Is there a more elegant solution for what I presented below? Do I have to write a function for each button? Also, I risk adding btn btn-defaultto a button that already has this class. Any help is appreciated.
$("#today").click(function () {
$(this).parent().find("btn btn-primary").removeClass("btn btn-primary");
$(this).parent().addClass("btn btn-default");
$(this).addClass("btn btn-primary");
});
source
share