user3154990
user3154990

Reputation: 565

How to change color of a button when clicked using bootstrap?

Can someone show me how to change a color of a button in bootstrap? I have a group of buttons and only the clicked button should be changing its color. Thanks.

Upvotes: 5

Views: 9570

Answers (1)

sir_thursday
sir_thursday

Reputation: 5409

Without posting any code the best I can do for you right now if give you a simple JQuery function to change a button's color by switching classes.

JS:

// .btn is the class of the element you want to change color
$(".btn").click(function() {
  // Instead of directly editing CSS, toggle a class
  $(this).toggleClass("clicked");
});

CSS:

.btn {
  background-color: red;
}

.clicked {
  background-color: blue;
}

EDIT: As ashin999 said, you actually have pre-defined classes in bootstrap (as you probably know) to change button stylizations. So you could toggle one of those classes instead of a new clicked class.

Upvotes: 5

Related Questions