How to change color one button for each click?

2

I need that by clicking on a button that is green, it turns red, and if it is red it turns green again. I currently use bootstrap it could be that when I click it is danger and when I hit it again it is primary. I know it's done with JS but I really do not know how. in advance thank you very much

    
asked by Sergio Bedoya 23.10.2017 в 01:14
source

1 answer

0

Use the .toggleClass() method of jquery where you can change one class or the other depending on which one is selected:

$("#mi-boton").click(function(){
  $(this).toggleClass("btn-danger btn-success");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://getbootstrap.com/docs/3.3/dist/css/bootstrap.min.css" rel=stylesheet>
<button id="mi-boton" class="btn btn-danger">boton</button>

In this example, depending on the click, change between the class .btn-danger or .btn-success .

    
answered by 23.10.2017 / 01:37
source