Modify the bg class when the checkbox is selected

Currently revamping my portfolio website and could use a hand with some jQuery code. Hoping to make a parent class react to a checkbox within it. Specifically looking to change the background color to #000 when the checkbox is checked.

Struggling to figure out which line of code to incorporate in order for my class to respond to the checkbox. Tried looking up tutorials online, but haven't made much progress. It's probably just a minor tweak that I'm missing, so any assistance would be greatly appreciated.

$("input[type='checkbox']").change(function() {
  if ($(this).is(":checked")) {
    $(this).parent().css('background-color', '#000');
  } else {
    $(this).parent().css('background-color', '#ccc');
  }
});
.product {
  background-color: #ccc;
  /* needs to change to #000 when active */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="product">
  <input class="check" type="checkbox" name="logo"> Logo 
</div>

Answer №1

To implement a dynamic active state for your products, you can create a more specific CSS selector and use jQuery to toggle the state based on the checkbox value.

Check out this example on how to achieve this: https://jsbin.com/betafupogu/1/edit?html,css,js,output

CSS

.product {
  background-color: #ccc; /* will change to #000 when active */
}

.product.active {
  background-color: #000; 
}

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>

        $("input[type='checkbox']").change(function(){
    if($(this).is(":checked")){
        $(this).parent().addClass('active'); 
    } else {
        $(this).parent().removeClass('active'); 
    }
});

</script>

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

HTML Canvas resizing challenge

My goal is to resize the canvas to fit inside its parent div within a Bootstrap grid, but I'm running into an issue where the canvas keeps expanding to 2000px x 2000px. Despite successfully resizing based on console.log outputs showing the same dimens ...

Component encounters issue with undefined props being passed

Encountering an issue where dummy data is not being passed to a component, resulting in undefined props. Even console.log() statements within the same page are not functioning properly. Can anyone identify what might be going wrong here? import AllPosts fr ...

What is the best way to connect the elements in two separate arrays?

I have a scenario with two arrays and a variable: var Names = ['jack', 'peter', 'jack', 'john']; var Ids = ['1' , '2' , '3' , '4' ]; Also, I have this search varia ...

What is the best approach for sending a binary image post request to an API using nodejs?

I am receiving the image binary in this manner: axios.get("image-url.jpg") Now, I want to utilize the response to create a new POST request to another server const form = new FormData(); const url = "post-url-here.com"; form.appe ...

Having trouble with Node JS res.redirect() not functioning as desired?

I'm attempting to use res.redirect() to redirect to an external URL, but the host name isn't being replaced. For example: My current URL: https://example.com Desired redirect URL: When I use res.redirect("https://example-2.com/pqr?par=123 ...

What is the best way to retrieve scope variables from multiple directives?

I am working on a directive that represents a person with changing location attributes. My goal is to access all the locations together and plot them on a map using the angular-leaflet-directive. I'm struggling to figure out how to access these variab ...

Running the JavaScript function '.hover' using Selenium WebDriver

My goal is to utilize Selenium WebDriver to run some javascript code from a webpage. I have come across several helpful posts on executing javascript, but I often struggle when trying to call javascript from page objects (I am still learning the terminolog ...

Utilize conditional styling along with hover effects in ReactJS

I'm facing an issue where I need to set up a conditional style and a hover effect for TableRows. They work fine individually, but when combined, the hover function stops working. CSS const StyledTableRow = withStyles((theme) => ({ root: { tab ...

Harmonizing various client viewpoints in a ThreeJS scene featuring a unified mesh structure

I am fairly new to ThreeJS and I am curious to know if it is possible to achieve the following, and if so, how can it be done: Two web browser clients on separate machines want to load the same html-based Scene code, but view it from different perspective ...

CSS deep level selectors

For the following HTML/CSS code, I am trying to target only level 1 "row" elements and not their nested counterparts. However, when using the child selector, it still selects the nested elements. How can I achieve selecting only level 1 "row" elements? ...

The JAVA REST Resource is being triggered twice when downloading a file

I have a piece of code that allows me to download a zip file using JavaScript and AJAX. On the back-end, I utilize REST resources and Java to process the request. Below is the JavaScript code for downloading the file. var urlDownload = "/test/download/"; ...

Grabbing <object> HTML using jQuery

One example on my webpage is the presence of the following <object>: <object id="obj1" data="URL"></object> Could you please suggest a method to access this html object using jQuery? ...

Include a condition to verify the values

Looking to enhance the code by incorporating an if or switch case: The initial code snippet is as follows: {(errorMessage || invalidEmailError.length > 0) && ( <Message type="error" className="mt-l" ...

Saving the selected value from a dynamic dropdown menu in a JavaScript application to a MySQL database

This code successfully saves the values of departments but is encountering an issue with saving the degree value into MySQL. I am currently using PHP for this functionality. Here is the HTML code: <select class ="form-control" name="depa ...

What is the best way to extract JSON data from an if statement?

Is there a way to access the 'data' variable outside of the if-else statement in the global scope? let request = new XMLHttpRequest(); request.open('GET', 'API Url'); request.send(); request.addEventListener('readystatec ...

Cloning a table row through editing functionality in ReactJS

Hey there, I'm currently working on implementing an edit feature in react.js. The current functionality is such that when the edit button is clicked, it displays the row data in the name and email address fields. If submitted without any changes, it c ...

Tips for efficiently displaying a computed property on a template using Vuetify?

I am attempting to display only the initials of a user who is logged in while inside a store. Here is my template: <v-menu v-if="this.$store.getters.getLoggedUser"> <template v-slot:activator="{ on, attrs, userInitials }&quo ...

Selecting elements with a wildcard using JQuery property selectors

After clicking my Up/Down arrows within a gridview, I am attempting to adjust the Row index. However, the issue I am facing is that my Jquery code is only able to target one of the gridviews inside my Repeater. The specific ID I am using to access the row ...

Static file serving is malfunctioning

I created a basic node project to work on, but I'm struggling to serve an HTML file with its accompanying CSS styles. Previously, this exact code worked without any issues, but for some reason, it's not functioning now. I've tried searching ...

What is the optimal method for saving a dynamic webpage as an object?

In my current project, I am developing a PHP application that utilizes MySQL for the database. One of the features I want to include is a site builder using AJAX to load and store dynamic page content. The goal is to allow users to make changes to the site ...