1

How do I make a button change to another color when clicked and when clicked again it goes back to its original color? Can I do by using CSS or do I have to use Javascript?

Thanks in advance

This is the HTML code of my buttons.

<div class="H1toH5">
  <button class="seatButton">H1</button>
  <button class="seatButton">H2</button>
  <button class="seatButton">H3</button>
  <button class="seatButton">H4</button>
  <button class="seatButton">H5</button>
</div>
0

5 Answers 5

6

Pure CSS

  1. Change your button to another element (like span)
  2. Use a label and checkbox to control the toggle status
  3. Use :checked CSS selector and + sibling selector

.H1toH5 input { display: none; }
.H1toH5 .seatButton { padding: 5px; border: 1px solid #ccc; background: yellow; }
.H1toH5 input:checked + .seatButton { background: red; }
<div class="H1toH5">
  <label>
    <input type="checkbox" />
    <span class="seatButton">H1</span>
  </label>
  <label>
    <input type="checkbox" />
    <span class="seatButton">H1</span>
  </label>
  <label>
    <input type="checkbox" />
    <span class="seatButton">H1</span>
  </label>
  <label>
    <input type="checkbox" />
    <span class="seatButton">H1</span>
  </label>
  <label>
    <input type="checkbox" />
    <span class="seatButton">H1</span>
  </label>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I would have done - the functionality asked for really seems like checkboxes (highlighting/selecting multiple items) not buttons that behave like checkboxes.
thanks..i already knew how to do this, i just didnt want to change all the buttons i'll guess i have to as this way is much easier!
3

How do I make a button change to another color when clicked and when clicked again it goes back to its original color? Can I do by using CSS

Try utilizing css :focus pseudo class , background-color

button {
  background-color:yellow;
}

button:focus {
  background-color:orange;
}
<div class="H1toH5">
  <button class="seatButton">H1</button>
  <button class="seatButton">H2</button>
  <button class="seatButton">H3</button>
  <button class="seatButton">H4</button>
  <button class="seatButton">H5</button>
</div>


no just the one that is clicked, but i want to be able to click on many buttons not just one

var elems = document.getElementsByClassName("seatButton");
for (var i = 0; i < elems.length; i++) {
  elems[i].onclick = function() {
    var color = window.getComputedStyle(this, null)
                .getPropertyValue("background-color");
    this.style.backgroundColor = color === "rgb(255, 255, 0)" 
                                 ? "rgb(255, 165, 0)" : "rgb(255, 255, 0)";
  };
};
button {
  background-color:yellow;
}
<div class="H1toH5">
  <button class="seatButton">H1</button>
  <button class="seatButton">H2</button>
  <button class="seatButton">H3</button>
  <button class="seatButton">H4</button>
  <button class="seatButton">H5</button>
</div>

3 Comments

this sort of works but it only selects 1 button at a time, i want to be able to click many buttons.
@Bane Is expected result for click on any single button changes all button elements background color ?
no just the one that is clicked, but i want to be able to click on many buttons not just one
2

You can do this with css a ::before pseudo element and the html checkbox input tag

input.toggle-btn {
    visibility: hidden;
}
input.toggle-btn::before {
    content: attr(value);
    display: inline-block;
    padding: 5px;
    background: #fff;
    font-size: 14pt;
    color: #aaa;
    border-radius: 5px;
    border: 1px solid #aaa;
    visibility: visible;
}
input.toggle-btn:checked::before {
    background: rgb(50,150,250);
    color: #eee;
    border-color: #eee;
}
<input class="toggle-btn" type="checkbox" value="Hello">

Comments

0

This code will toggle a specific class when you click on it. If you click on the button again it will then return to it's original state.

    $('.seatButton').click(function () { 
       $(this).toggleClass('activeClass');
    }); 

Or just use the CSS Pseudo selected as people have specified above. There are many ways to achieve what you want!

1 Comment

@humble.rumble - True. Didn't read the answer properly. He could just do a toggleClass(); function.
0

No. You need to add/remove a class with javascript. To make @tpie feel better about himself, here is the code for that. Using jQuery:

$('.seatButton').on('click', function(){
    $(this).toggleClass('is-active');
});

4 Comments

Sure... if you want to make it a checkbox instead. The question was to use a button tag. I am well aware of how to do it with a checkbox or radio button.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@tpie Umm... this EXACTLY answered the question. The question... "Can I do by using CSS or do I have to use Javascript?" My answer... "No. You need to add/remove a class with javascript."
Sean, please review the OP: "How do I make a button change to another color when clicked and when clicked again it goes back to its original color?" Your answer does not actually explain anything, and as shown by other answers, there are other solutions. If you want to insist it is an answer, fine, but it is of no practical value. You should delete it before it gets down-voted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.