I seen the multiple thread on stackoverflow related the same title of this post but this different which I am going to ask.
I am trying to make code in jQuery that suppose my default button color is gray and I wish I click one time It will convert to green and when I click twice on btn or click green btn again it will becomes red.
overall when I single click on btn it will green and when I click double it will red and when I click third it will default color as grey OR when I click green it will red and when click red it will grey
I want to post this btn data when If btn is grey then btn value = 0 same when green == 1 and when red == 3
I tried to make code but I only know single click function without btn value.. Please help me to creating this code..
jQuery("#my_styles .btn").click(function() {
jQuery("#my_styles .btn").removeClass('active');
jQuery(this).toggleClass('active');
});
.active{
background:red;
}
button.btn:active{
background:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
HTML--
<div class="col-sm-12" id="my_styles">
<button type="submit" class="btn btn-warning" id="1" value="0">Button1</button>
<button type="submit" class="btn btn-warning" id="2" value="0">Button2</button>
</div>
<div class="col-sm-12" id="my_styles">
<button type="submit" class="btn btn-warning" id="3" value="0">Button3</button>
<button type="submit" class="btn btn-warning" id="4" value="0">Button4</button>
</div>
btn-redandbtn-green. You already have a value attribute on your button. Update the value tocurrentValue+1each time the button is clicked. Based on the value0 = no class,1 = btn-greenand2 = btn-red. After the value is greater than 2 reset it to 0. Hope that helps!