Is there a way to alter the background color of an entire section once a child element within it is clicked?

I am currently working on a personal project where I am attempting to replicate the design shown below:

The most challenging part for me right now is implementing the functionality that when one of the 6 differently colored boxes is clicked, additional information pops up like in the example:

My initial focus is on changing the background color when the Facebook Ad Campaign box is clicked. Upon clicking it, the background color of the entire container (which houses the 6 boxes) should change.

I believe using jQuery is the best approach for this task, but my attempts so far have been unsuccessful as the following code did not work:

$("#fbAdCampaigns").click(function(){
$(#container-parent").backgroundColor = "red";
}

Alternatively, I tried the following code to see if it changes the color of the first of the 6 boxes:

 $("#fbAdCampaigns").click(function(){
$("#fbAdCampaigns").css({ "background-color": "#ffe"});
})

Unfortunately, neither of these methods worked as expected since nothing happened. You can view what I have accomplished so far here:

https://codepen.io/JoyFulCoding/pen/EzWyKv

I greatly appreciate any assistance you can provide.

Answer №1

There seem to be a few issues that need addressing.

  1. Please remove the <script> tags from your HTML and place them in an external JavaScript file after all imports.
  2. It is sufficient to have just one document.ready function for your code.
  3. Instead of using .click, consider using .on('click', function() ...).
  4. When applying CSS styles, use .css('attribute', 'value') instead of .css('attribute': 'value').

For better organization, avoid inline CSS and Javascript if you are already using separate CSS and JS files.

Here is a snippet of corrected code that may not meet all your requirements but can guide you:

$(document).ready(function() {

  $("#menu").on('click', function() {
    $("#icon").toggleClass("fa-times");
  });

  $("#fbAdCampaigns").on('click', function(){
    $("#fbAdCampaigns").css("background-color", "#000000");
  });

});

Answer №2

The container's color change may not be noticeable due to the presence of six boxes positioned on top of it. A solution could involve altering the color of each box individually upon being clicked:

Explore this example for reference

function adjustColorScheme(){
  const selectedColor = this.style.backgroundColor;
divElements.forEach(element=>element.style.backgroundColor=selectedColor)
}

Answer №3

If you want to modify the CSS of the #fbAdCampaigns element, there are a couple of options available to you. You can either add a class and handle the changes using CSS, or use the .css() method.

$("#fbAdCampaigns").on('click', function() {
  $("#fbAdCampaigns").addClass("bg-color");
});

$("#fbAdCampaigns").on('click', function() {
  $("#fbAdCampaigns").css("background-color", "#fff");
});

By using .addClass, you have the flexibility to easily revert the change by using .removeClass. Another approach would be to utilize toggleClass for more dynamic behavior.

$("#fbAdCampaigns").on('click', function() {
  $("#fbAdCampaigns").removeClass("bg-color");
});


$("#fbAdCampaigns").on('click', function() {
  $("#fbAdCampaigns").toggleClass("bg-color");
});

If you need to target the entire container, you can make use of the jQuery method .parent()

https://api.jquery.com/parent/

For a live example, check out this link: https://codepen.io/anon/pen/arwZZm

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

Unable to retrieve information from compact JSON files

It's been 2 hours and I still can't figure this out I need help with reading my Json Data in my JavaScript File, saving it to a variable, and then printing it out. I've tried multiple solutions but nothing seems to work (I'm new to ...

My jquery is malfunctioning when trying to retrieve values

I am facing an issue with accessing server-side values in my jQuery function. When I use my localhost path (NewsRecord.php) as the AJAX URL, it works fine. However, when I use the server path, it does not work. The strange thing is that the server URL pr ...

CF component not able to accept arguments when invoked by JavaScript through cfajaxproxy

Ever since updating to ColdFusion 2016 Update 4, I've been experiencing a new issue. Here's the code snippet: <input type='button' name='btn' value='Click me' onclick='proxyFunc();'> Incorporating ...

Utilize Lodash to iterate through functions in a loop and retrieve the first matching result

I am looking to iterate through an array of objects and call a method on them. If the result of that method meets certain conditions, I want to immediately return that result. The current implementation is as follows: public getFirstMatch(value: string, a ...

Encountering issue with Django locating static file (1.5) showing ERROR 404 code 1649

I'm facing some difficulties in setting up my local Django site. I've reviewed the previous questions and answers, but haven't been able to find a solution. [20/Oct/2013 03:56:33] "GET /rides/ HTTP/1.1" 200 230 [20/Oct/2013 03:56:33] "GET / ...

The JQuery datepicker fails to provide the date in the format mm/dd/yy

Recently, I attempted to transform a date into the dd/mm/yy format using JQuery datepicker. Unfortunately, my results displayed in the dd/mm/yyyy format instead. Here is the code snippet that I utilized: chkIn = $.datepicker.formatDate("dd/mm/yy", cinDate ...

Show the result as "Content-Type: image/jpeg" on the webpage

I have a URL for an API request that automatically downloads and saves a QR code image from the browser. The Content-Type of the URL is application/jpeg, and its format looks like this: application(websiteURL)/egs?cmd=gen_qrcode&customer_id=123&n ...

What occurs when a numerical value is added to an element within an array?

Illustration 1: Let numbers = [4,5,6] + 2; console.log(numbers) // "4,5,62" typeof numbers // String Illustration 2: let nums = 10 + ["w","o","r","l","d"]; console.log(nums) // "10w,o,r,l,d"; typeof nums // String When combining a number or string wi ...

Leveraging Jquery Splice

Trying to eliminate an element from an array using the Splice method. arrayFinalChartData =[{"id":"rootDiv","Project":"My Project","parentid":"origin"},{"1":"2","id":"e21c586d-654f-4308-8636-103e19c4d0bb","parentid":"rootDiv"},{"3":"4","id":"deca843f-9a72 ...

Retrieve documents from mongodb that were created in the past half hour

Currently, I am in Central European Summer Time (CEST) and at 15:08, I added a document to my collection. When passing timestamps: true to a new Schema, the createdAt time is showing two hours too early: createdAt:2019-08-21 13:08:39.219 Why is this happ ...

What is the best way to customize the primary Button style in Bootstrap?

Would you like the background-color to change when clicking on the radioButton? .btn-primary:hover, .btn-primary:active, .btn-primary:visited, .btn-primary:focus { background-color: black !important; color: white !important; } .btn-primary { colo ...

Update the website URL in the browser using jQuery

Here is my code for making AJAX requests in my app: $(document).ready(function () { $('ul#ui-ajax-tabs li:first').addClass('selected'); $('#ui-ajax-tabs li a').click(function (e) { e.preventDefault(); ...

Customize Your Google Maps with Checkboxes for Style Options

Trying to make dynamic changes in the style of a Google Maps using checkboxes. Some checkboxes control colors of features, while others control visibility. Having trouble figuring out how to combine different features of styles since they are not strings ...

Tips for showcasing the individual product page in full width on woocommerce

Here is a link to the single product page: I am wondering if it's possible to extend the product summary drop-down menus all the way to the right without impacting the mobile site? Thank you. ...

Creating personalized components - a step-by-step guide

I'm looking to customize the appearance of the snackbar background, buttons, and other elements in my Material UI project. As a newcomer to Material UI, I'm unsure if I'm taking the correct approach with this code snippet: const styles = { ...

manage user assignments for multiple events in Django

Struggling to create a method for admin users to assign multiple events to users within the event details. The goal is to allow users to be associated with many events, and events to have multiple users linked to them. Unfortunately, I haven't been ab ...

What are the advantages of utilizing buffer geometries in Three.js?

I have experience using both BufferGeometry and Geometry, so I feel comfortable with either. Even when I need to make frequent modifications, I tend to lean on BufferGeometry because although the code is more verbose, it's not overly complex. Can you ...

Exploring Various JSON Arrays

There are 3 arrays provided below which I have no control over: groups_array = [ { "group" : "Mobile Test Region", "id" : "251" }, { "group" : "Mobile Demo Region", "id" : "252" } ] locations_array = [ { "location" : "M Testing", " ...

Obtain the value of a specific input element across several forms using jQuery

There are several forms present on a single page. A button has been created to toggle the visibility of each form, and it is functioning correctly. The issue is that I am attempting to include the value from the #family_name input field after the button to ...

Is there a way to modify the props.data in a child component?

I need assistance with displaying a range from an array that is passed into a child component. Currently, my Parent component looks like this: import data from './data.json' return ( <Cell symbol={data.symbol} number={data.number} /> ) ...