I have a button that does the following:
- When pressed, the button removes a class (1) and adds another one (2). This works.
- When pressed again, I want to remove the second class (2) and add the first one (1). This does not work.
Sort of like a toggle.
This is the code:
HTML:
<button class="follow_btn btn btn-success" value="1">Follow</button>
CSS:
.btn {
padding:1em;
}
.btn-success {
background:green;
}
.btn-danger {
background:red;
}
jQuery:
$('.follow_btn').on("click", function () {
var this_btn = $(this);
this_btn.removeClass("btn-success follow_btn").addClass("btn-danger unfollow_btn");
});
$('.unfollow_btn').on("click", function () {
var this_btn = $(this);
this_btn.removeClass("btn-danger unfollow_btn").addClass("btn-success follow_btn");
});
and a JSFiddle showing my problem:
http://jsfiddle.net/thedarklord1939/pGuue/
Why is this doing not working? If this is not clear enough, I will elaborate. Thank you.