Achieving CSS Buttons That Change to Green When Clicked and STAY Green

I've been tasked with working on a front-end project for a website that involves buttons changing color when clicked and staying that color.

Here is the CSS <style> code I have written for these specific buttons:

.button {
    background-color: #ffffff;
    border: 1px solid #3e1313;
    color: #3e1313;
    padding: 10px;
    text-align: center;
    text-decoration: none;
    font-size: 12px;
    margin: 4px 2px;
}

button:hover{background-color:orange;}

.button4 {border-radius: 10px;}

HyperText:

<div class="row">
  <div class="columnExpand">
    <label class="container" name="lblDriverExperience">
      Driver Experience
      <input type="checkbox" name="chkDriverExperience" 
             onchange="divToggle('divDriver')">
      <span class="checkmark"></span>
    </label>
  </div>
  <div class="columnOption" id="divDriver">
    <button class="button button4">Good</button>
    <button class="button button4">Fair</button>
    <button class="button button4">Poor</button>
  </div>
</div>

JAVASCRIPT:

<script type="text/javascript>
  $("#test").click(function() {$(this).toggleClass( "active")})
</script>

While the button currently turns brown on hover, my goal is to make it turn green when clicked and stay green. I've searched for solutions but haven't found one that works.

Answer №1

To change the appearance of a button, you can add or remove the class .active

$(".columnOption .button").click(
  function() {$(this).toggleClass( "active")}
)

$(".columnOption .button").click(
  function() {$(this).toggleClass( "active")}
)
.button {
    background-color: #ffffff;
    border: 1px solid #3e1313;

    color: #3e1313;
    padding: 10px;
    text-align: center;
    text-decoration: none;

    font-size: 12px;
    margin: 4px 2px;
}

button:hover{background-color:orange;}

.button4 {border-radius: 10px;}

.columnOption .button.active {
    background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="columnExpand">
    <label class="container" name="lblDriverExperience">
      Driver Experience
      <input type="checkbox" name="chkDriverExperience" 
             onchange="divToggle('divDriver')">
      <span class="checkmark"></span>
    </label>
  </div>
  <div class="columnOption" id="divDriver">
    <button class="button button4">Good</button>
    <button class="button button4">Fair</button>
    <button class="button button4">Poor</button>
  </div>
</div>

Answer №2

Here is a simple solution that may suit your needs:

<script type="text/javascript">
$( ".button" ).click(function() { $( this ).css( "background-color", "green" )} );
</script>

You can see it in action on this Fiddle: https://jsfiddle.net/CriticalError/jLst5owk/

Answer №3

div label input {
   margin-right:100px;
}
body {
    font-family:sans-serif;
}

#ck-button {
    margin:4px;
    background-color:#EFEFEF;
    border-radius:4px;
    border:1px solid #D0D0D0;
    overflow:auto;
    float:left;
}

#ck-button label {
    float:left;
    width:4.0em;
}

#ck-button label span {
    text-align:center;
    padding:3px 0px;
    display:block;
}

#ck-button label input {
    position:absolute;
    top:-20px;
}

#ck-button input:checked + span {
    background-color:#911;
    color:#fff;
}
<div id="ck-button">
   <label>
      <input type="checkbox" value="1"><span>red</span>
   </label>
</div>

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

Error: The JSON input unexpectedly ended, however the PHP file itself is error-free

When trying to display data retrieved using PHP through JSON/Ajax, I encountered an error message: [object Object] | parsererror | SyntaxError: Unexpected end of JSON input The PHP script is functional (I can view the JSON output by directly accessing th ...

Retrieving JSON data from a PHP file through a standalone JavaScript script

I am in need of creating a JavaScript file that will assign values to two variables within a PHP file. The PHP file should then construct a JSON structure and display some data. My goal is to have a form written in JavaScript with two input fields that wi ...

The outcome of a Wordpress AJAX submission redirects to the admin-ajax.php file rather than appearing in the designated content

I've encountered an issue with the AJAX handler in WordPress. Here is the form that's causing trouble: form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter" name="filter"> <input type="h ...

Currently utilizing Yii framework to customize the appearance of a DIV element by specifying the CSS properties within the View, and subsequently storing the CSS file

I am looking for a way to allow users to customize the properties of a parent div, such as color, background, and other CSS styles, directly from the WordPress theming dashboard interface. Can someone please guide me in the right direction? ...

The issue with React Testing using Jest: The domain option is necessary

I've recently developed a React app that implements Auth0 for Authentication. Right now, I'm working on setting up a test suite using Jest to test a specific function within a component. The function I want to test is createProject() in a React ...

Changing the input to uppercase within Vue.js

I am looking to capitalize the input data from the user's full name model and would prefer if it is in Latin letters only. Utilizing Vue.js for Uppercase Conversion <p-input :value="value" @input="$emit('input', $event)&qu ...

Icons are failing to display in the dropdown menu of MUI after an option is selected in ReactJS

There seems to be an issue with the icon in the select tag. Initially, it is not showing which is correct. However, when you click the dropdown, the icon appears as expected. But after selecting an option, if you click the dropdown again, the icon disapp ...

Having difficulty using replaceWith to replace text in jQuery

My favorite script successfully adds data to SQL and replaces HTML tags. I have included a text like Add Favorite and used replaceWith to display Remove Favorite. However, the output is not as expected, as shown in the image below. https://i.sstatic.net/x ...

How to place a button in the bottom center of a panel using asp.net

After learning about centering a button at the bottom-center inside a div, it became clear that it's similar to centering a button within a panel. I managed to achieve this by adding the following CSS to the button: position:absolute; margin-left ...

What are some solutions for resolving the problem with the anchor attribute in Safari?

There is a problem with the anchor tag used in my website. The number 619-618-1660 is being displayed when I view the site on Safari browser or an iPhone, even though it is written as (href="tel:619-618-1660"). See the image at the link below for reference ...

What is the reason the useEffect hook does not function properly with a state variable within context?

Check out my code here. I'm trying to display the content of the array testingData, but it's not showing up. If I remove the useEffect hook, it works fine. Can you help me understand why and how to fix it? ...

Vue transition unexpectedly ending in the wrong position due to nested transitions

I've encountered an issue while trying to implement a transition on a child div within a 'parent' div that already has its own transition. It seems like the child transition is conflicting with the parent transition, causing it to end up in ...

Implementing an animation feature to facilitate the item filtering process when clicked on

I have recently developed a filter gallery and I am encountering an issue with animating the filter items when clicking on buttons. The problem with my current code is that it only toggles the animation effect rather than smoothly animating each time a but ...

When an AJAX call is made during a PHP session that has timed out

I am working on an AJAX form that handles data authentication. In the event of a session timeout, I need to implement a redirect to the login page. How can I go about achieving this? Here is an excerpt from my simplified server-side code: function doExecu ...

Generating an array in Javascript using JSON

I've been doing a lot of research, but I can't seem to find a solution that works for me. My goal is to retrieve the next four bus arrival times for a specific bus stop using an application that reaches out to an API returning JSON data. The issu ...

Tips for transforming intricate JavaScript arrays into a unified array and iterating through to create a table

I need help combining two arrays that contain objects and reassigning the values in a standard format. Can anyone provide some guidance? Thank you. Here is my current situation: array1 = [{Apple: Ken, Orange: Mary, Melon: Tina, Strawberry: Jessica, Mango ...

What is the best way to invoke a TypeScript function within a jQuery function?

Is it possible to invoke a TypeScript function within a jQuery function? If so, what is the correct approach? Here is an example of my component.ts file: getCalendar(){ calendarOptions:Object = { height: 'parent', fixedWeekCount : ...

Update the variable in a PHP script and execute the function once more without the need to refresh the page

Hey there, I'm wondering if AJAX is necessary for the functionality I want to implement. Here's the scenario: I have a function that generates a calendar and I plan to add 'forward' and 'backward' arrows so users can navigate ...

Checking form input using jQuery Ajax

After validating a form and submitting it via ajax, errors like existing user names need to be displayed next to the relevant form elements. The form's structure is as follows: <span class="inputRow"> <span class="formLabel"> ...

The back button stops functioning properly following a push state operation

When the user clicks the back button, there is an issue with the page reloading. Ideally, the previous page should be displayed but instead, the same content keeps appearing. [url removed] ...