Reputation: 1248
If someone visit first time, I need to show red color active button. Then he switch between yes and no, I need to show another active color. Please look at the screen shots.
First time
Switching between yes and no
I tried with this code. It's only switching active class.
$('.btn-switch').click(function() {
var $group = $(this).closest('.form-group');
$('.btn-switch', $group).removeClass("active");
$(this).addClass("active");
});
.btn-switch.active {
background: #e46a5d!important;
color: #fff;
font-weight: normal;
text-shadow: none;
}
.btn {
padding: 6px 51px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="form-group">
<div class="btn-group btn-group-justified btn-switch-justified">
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch active">
<span class="goods">Yes</span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch">
<span class="services">No</span>
</button>
</div>
</div>
</div>
Do I need to add another active class or any other method for doing this?
Upvotes: 0
Views: 1261
Reputation: 16855
You have to switch the classes based on the condition.
Stack Snippet
/* Latest compiled and minified JavaScript included as External Resource */
$('.btn-switch').click(function() {
$('.btn-switch').removeClass("active red blue");
if ($(this).find('span').text() == "Yes")
$(this).addClass('active red');
else
$(this).addClass('active blue');
});
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.btn-switch.active {
background: #e46a5d!important;
color: #fff;
font-weight: normal;
text-shadow: none;
}
.btn-switch.active.red {
background: #e46a5d!important;
}
.btn-switch.active.blue {
background: blue !important;
}
.btn {
padding: 6px 51px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="form-group">
<div class="btn-group btn-group-justified btn-switch-justified">
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch active">
<span class="goods">Yes</span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch">
<span class="services">No</span>
</button>
</div>
</div>
</div>
Upvotes: 1
Reputation: 1296
If I understand you correctly, this is what you want:
$('.btn-switch').click(function () {
var $group = $(this).closest('.form-group');
$('.btn-switch', $group).removeClass("active").addClass("switching");
$(this).addClass("active");
});
.btn-switch.active{
background: #e46a5d !important;
color: #fff;
font-weight: normal;
text-shadow: none;
}
.switching.active{
background: #adf !important;
}
.btn{
padding: 6px 51px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="form-group">
<div class="btn-group btn-group-justified btn-switch-justified">
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch active">
<span class="goods">Yes</span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default btn-switch">
<span class="services">No</span>
</button>
</div>
</div>
</div>
Upvotes: 1