Struggling with altering the button color using CSS and JavaScript

I am struggling to change the color of my button on selection using Javascript. I have tried various methods such as adding ".active", ":active" and even using "!important", but nothing seems to work. The desired color for the button is #E68352. Below is the CSS code for the button:

.booking-container-section {
  width: 100%;
  margin-top: 20px;
  border: 1px solid rgba(255, 255, 255, .5);
}

.booking-container-area {
  margin: auto;
  height: 350px;
}

.btn {
  background-color: #423F3E;
  height: 30px;
  width: 30px;
  margin: 5px;
  border-radius: 10px;
  border: none;
  float: left;
}

.btn:active {
  background-color: #E68352 !important;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8>;
  <link rel="stylesheet" href="try3.css">
  <script type="text/javascript" src="booking.js"></script>
</head>

<body>

  <div class="container">
    <div class="content-container">
      <div class="specific-movie-content">
        <div class="specific-movie-img">
        </div>
        <div class="specific-movie-desc">
          <h1 class="specific-movie-title"></h1>
          <p class="specific-movie-para"></p>
        </div>
      </div>
      <div class="booking-container">
        <div class="booking-container-section">
          <h1 class="booking-seats-font">SEATS</h1>
          <div class="booking-container-area">
            <div class="box">
              <p class="screen-font">SCREEN</p>
            </div>
            <div class="seats-layout">
              <input type="button" id="button1" class="btn" />
              <input type="button" id="button2" class="btn" />
              <input type="button" id="button3" class="btn" />
            </div>
            <p>
               You have selected <span id="display">0</span> seats</p>

            <!--script src="booking.js"></script-->
            <script>
var buttons = document.getElementsByClassName("button");
var count = 0;
var disp = document.getElementById("display");
for (let i = 0, l = buttons.length; i < l; i++) {
  buttons[i].addEventListener('click', function() {
    buttons[i].classList.toggle('active');
    if (this.classList.contains("active")) {
      count++;
    } else {
      count--;
    }
    disp.innerHTML = count;
  })
}
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

Answer №1

If you want the button to remain active, you need to include the button:focus in addition to :active.

var buttons = document.querySelectorAll(".btn");

buttons.forEach(function(el) {
 el.addEventListener("click", function() {
   el.classList.toggle("active");
 });
});
.booking-container-section{
    width: 100%;
    margin-top: 20px;
    border: 1px solid rgba(255,255,255,.5);
}

.booking-container-area{
    margin: auto;
    height: 350px;
}

.btn {
    background-color: #423F3E;
    height: 30px;
    width: 30px;
    margin: 5px;
    border-radius: 10px;
    border: none;
    float: left;
}

.btn.active {
  background-color: #E68352 !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8>
    <link rel="stylesheet" href="try3.css">
    <script type = "text/javascript"  src = "booking.js" ></script>
</head>
<body>
    
    <div class="container">
        <div class="content-container">
            <div class="specific-movie-content">
                <div class="specific-movie-img">
                </div>
                <div class="specific-movie-desc">
                    <h1 class="specific-movie-title"></h1>
                    <p class="specific-movie-para"></p>
                </div>
            </div>
            <div class="booking-container">
                <div class="booking-container-section">
                <h1 class="booking-seats-font">SEATS</h1>
                    <div class="booking-container-area">
                    <div class="box"><p class="screen-font">SCREEN</p></div>
                        <div class="seats-layout">
                        <input type="button" id="button1" class="btn" />
                        <input type="button" id="button2" class="btn" />
                        <input type="button" id="button3" class="btn" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

npm installation for phonegap failed due to shasum check discrepancy

I've attempted numerous times, but I keep encountering this error (shasum check failed) 4784 error Error: shasum check failed for C:\Users\FENGXI~1\AppData\Local\Temp\npm-7004-QbpFFte5\1387269030233-0.28223602287471 ...

Having trouble getting the tailwindcss Google font link tag to apply properly

When I use the @import in the tailwind css file, it works fine. However, when I try to add the font using the <link> tag in _document.jsx, it fails to work properly. In Chrome dev tools, it shows that the classes are applied but the fallback font is ...

What is the correct way to utilize the submit() function within this specific form?

I have a small registration form that requires validation before submission. In order to achieve this, I've developed a JavaScript function as shown below: var name = document.getElementById('name').value; var company = document.getElem ...

Organizing an array of objects within MUI cards based on social media platforms

I'm currently developing an application that showcases athlete data in a grid layout using MUI Grid. The colored borders on the left side of each card are determined by the corresponding social network associated with that athlete. https://i.sstatic. ...

Clear all events from an HTML element and its descendants with TypeScript

Each time the page loads, I have HTML from an API that is constantly changing. Is there a way to strip away all events attached to it? The original HTML looks like this: <div id="content"> <h2 onclick="alert('hi');">Test 1< ...

When hovering over the image, it enlarges and gradually becomes transparent, unveiling the hidden text beneath

I've been struggling to achieve the desired effect for quite some time now, putting in hours of effort without success. Essentially, I am aiming to create a div that showcases an image. When the mouse hovers over the image, it should zoom in and fade ...

Combining Node and React: Using CSS Class Names with Hyphens

Currently, my project utilizes react-starter-kit and includes several react components, each with its own styling file (scss). Every component imports the styling file at the top like this: import s from './Header.scss'; When it comes to using ...

Issue with the alignment of DIV container due to spacing issues

In the screenshot below, there is a DIV container with padding of 15px on the left and right. However, the contents are appearing spaced out downwards for some reason... Other than the left & right padding, no other styling seems to be affecting it. Adjus ...

Steps for including a header and footer with page numbers in an HTML page without disrupting the rest of the content when printing

When I print a page with a table that spans multiple pages, I want to ensure that my header and footer are included on every page. The code I've tried so far has treated the entire content as one continuous page. How can I achieve this? ...

Challenges with integrating PHP and JavaScript technologies

I am currently facing a challenge with transferring data from a PHP file to Javascript. I have set up a PHP file to pull data from a database and display it in HTML. My goal is to load this data when the page loads, store it in JavaScript arrays, and acc ...

Enlarging the current division while reducing the size of the others when hovering

Currently, I am working on a school project that involves creating a slideshow on a webpage. However, I have reached a point where I am unsure of how to proceed. Here is the progress I have made so far: body { background-color: #252525; } #wrapper { ...

Is HTML5 Ajax Upload Degrading?

Recently, I came across a fascinating technique for uploading files using Ajax: http://www.sitepoint.com/html5-javascript-file-upload-progress-bar/ This method has piqued my interest and raised a couple of questions in my mind. Surprisingly, the tech ...

Selecting JavaScript file on change as default option

Currently, I have an HTML file that prompts the user to select an XML file when the file selection is changed. Is there a way to make it so that by default, only one file is chosen? <form id='fileSelection' action=""> <center> <in ...

Having issues with my JavaScript timer - seeking assistance in troubleshooting the problem

Trying to set a timer for my little game, but facing some difficulties. The timer causes the balance to randomly increase from 0 to 13000 + some extra amount. <script> var coins = 0; var speed = 1; </script> <center> <h4> ...

Conflicting banner element positioning due to scroll bar pages interference

Is there a way to prevent elements from moving when navigating to a page with a scroll bar? I've observed that my elements shift slightly when selecting a page with a scroll bar. See an example here. I am considering forcing a scroll bar to appear on ...

Fixing Half Screen Sidebars

I have a query regarding my coding problem. I am trying to create two pop-ups that occupy half of each screen. As I am new to JavaScript and jQuery, I want to ensure that I am doing it correctly. Is there a way for the left side to slide out from the left ...

Angular Validators are behaving inconsistently, as they only work intermittently before stopping altogether once the application is

Why does my code sometimes behave differently after running for a while and then return to normal after restarting the Angular server without making any changes?The pop-up that says "please fill out this field" disappears when it shouldn't This is th ...

JSON syntax error: "r" is not a valid token at the beginning position

Currently, I am in the process of developing a web server that is based on STM32 MCU. The workflow involves the browser sending a request to the MCU, which responds with a web HTML file. Users can then adjust parameters and use a form to submit them back t ...

Troubleshooting the Checkbox Oncheck Functionality

Before checking out the following code snippet, I have a requirement. Whenever a specific checkbox (identified by id cfc1) is clicked, it should not show as checked. I have implemented the onCheck function for this purpose, but I'm struggling to fig ...