Update DIV Background Based on Nearest Radio Selection

Is there a way to make selecting the radio box easier with this code I have?

$("div.column43").click(function () {
$(this).find('input:radio').attr('checked', true);
});

I'm trying to figure out how to change the background and border of the div when the radio inside it is selected.

Although I can get the border to change on Hover, I want it to stay changed until it is deselected.

This is an example of the structure on the page:

<div class="row43">
                <div class="column43">
                    <input id="box21" type="radio" name="box21" 
value="" checked> <label for="box21">Any/All</label>    
                </div>
                <div class="column43">
                    <label for="box22">1.0</label><input 
id="box22" type="radio" name="box21" value="1.0">
                </div>
                <div class="column43">
                    <label for="box23">1.1</label><input 
id="box23" type="radio" name="box21" value="1.1">   
                </div></div>

Here's the CSS for the hover effects:

div.row43:hover > div {
opacity: 0.5;
}
div.row43:hover > div.column43:hover {
opacity: 1.0;
}
div.row43:hover {
border-color: #0099ff;
}
div.column43:hover {
opacity: 0.5;
border-color: #0099ff;
}

I'm struggling to keep the border effect on when the radio is selected and the mouse is moved away. Any suggestions would be greatly appreciated!

If anyone could provide guidance using the actual classes, that would be amazing. Thank you!

Answer №1

To achieve the desired result of having labels appear after radio buttons, you can utilize CSS only:

input[type="radio"]:checked+label {
    background:#faa;
    border:1px solid red;
}

Here is a jsFiddle example demonstrating this.

If placing labels after radio buttons is not feasible, jQuery can be used to accomplish it as shown below:

$("div.column43").click(function () {
    $(this).parent('div.row43').find('label').removeClass('highlight');
    $(this).find('input:radio').attr('checked', true);
    $(this).find('label').addClass('highlight');
});

Check out this jsFiddle example for a demonstration of using jQuery.

Answer №2

To implement functionality for radio buttons, first create an event and then modify the parent element's background color using jQuery.

$('div.section21').find('input[type=radio]').on('change', function() {
  if ($(this).is(':checked')) {
    $(this).parent().css('background-color', 'blue');
  }
});

Answer №3

Give this a try:

$(':radio:checked').closest('div').addClass('highlighted');

Or you can also use .css() method:

$(':radio:checked').closest('div').css({
                                        'border':'solid 1px blue', 
                                        'background': '#f0f0f0'
                                       });

And here is how the final code should look like: view fiddle example

$(':radio').change(function () {
    $(this).parent().removeClass('highlighted');
    $(':radio:checked').closest('div').addClass('highlighted');
});

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

Guide for using a CSS variable in a dynamic CSS class within a dynamic component

I'm working with library components and running into an issue with CSS when importing the same component multiple times within a parent component with different styles. import "../myCss.css" const CircleComponent = ({size , color}) => { ...

Final day of the month (without a time stamp)

Looking to generate two dynamic dates in HTML, JavaScript, and jQuery with the format yyyy/mm/dd. One should display the last day of the previous month, and the other the last day of the current month. Does anyone have a solution using these technologies? ...

Following the upload process, my webpage is mistakenly referencing the wrong directories

After completing a mock site project, I encountered an issue where the styling was perfect locally but did not show up once uploaded to my server and Github pages. Looking into the console, I found this error message: Failed to load resource: the server ...

Customize the focus color of mat-label within a mat-form-field in Angular

Within my mat-form-field, I have a mat-chip-list set up similar to the example provided in this link. When I click inside the field, the mat-label and what appears to be the border-bottom (not entirely sure) change color when focused. How can I customize t ...

Pictures in the portfolio failing to load on Mozilla Firefox

Having some trouble with my WordPress website bmfconstruction.com.au/new/. In the project section, all images are loading correctly in Chrome, Opera, and IE. However, when it comes to Firefox, none of the images seem to be showing up. I've tested this ...

Trigger Form Submission When Checkbox is Modified

Here is the PHP code I am working with: I need to update a value in the database immediately when a checkbox is checked or unchecked. I am looking to accomplish this using either PHP or Javascript. <?php session_start(); include("head.php"); inclu ...

Incorrect positioning of dropdown menu when using Bootstrap 4 and translate3d() function

Having multiple bootstrap 4.4.1 dropdown menus, each within a column which is functioning correctly except for the position calculation. Example of HTML col: <div class="container"> <div class="row"> <div class="col"> <div ...

"Using Bootstrap's toast feature repositions every element on the

I am working on a website project and encountering an issue with a bootstrap toast. The toast currently appears in the top right corner, but I would like it to display above the carousel without affecting the layout. Instead, when the toast appears, it shi ...

Looking for a specific search term within the middle of strings in an array

Is there a way to improve the autocomplete feature of my input field so that it shows suggestions based on any part of the word typed in? I currently have it set up to only show suggestions if the input matches the start of a word in the array. How can I m ...

enhancing the capabilities of a text input field

How can I make a page load after a textbox loses focus? I tried this code but it's not working. What am I doing wrong? $(function() { $("#txtBox").blur(function() { $('#LoadPage').load("Defult.aspx"); }); }); ...

Increase the value only when there is existing code present

I currently have a MySQL table with two columns. The first column stores IP addresses, while the second column holds integers that need to be incremented. Whenever a new IP address visits the website, the integer associated with that IP should increase by ...

The link to add a project to the web app through Firebase is experiencing technical difficulties

When looking to integrate firebase into a web application, it provides links to include in the HTML file. I followed these instructions by copying and pasting the links into my project, but encountered an error in the console. After closely examining the l ...

Youtube no longer supports looping videos by adding "loop=1" to the URL

I'm trying to embed a YouTube video in my WordPress website with the loop parameter set to 1 so that it replays automatically. However, the video is not auto-replaying and instead showing related videos. Below is the HTML code I am using: <iframe ...

Which element to use for creating a color palette - <img>, <div>, or <canvas>?

I am currently in the process of developing a color-picker component using HTML, CSS, and Javascript. One key question that I have is how to best implement a color palette similar to the one below: Putting aside browser compatibility concerns with IE8 or ...

Only show the remove option when the value being updated is a duplicate

This snippet updates a database table field to "approved", regardless of the current value. The challenge here is to only display the button in the HTML form below if the value being updated is anything other than "approved". We don't want users to se ...

Enhance CKEditor with Linked Select Boxes Plugin

I have ventured into writing a CKEditor Plugin and have grasped the basic concepts. For instance: CKEDITOR.dialog.add( 'addDocumentGroupDialog', function ( editor ) { return { title: 'Link to a document group', min ...

Enhance the functionality of your current JavaScript code by adding value during the change event

Looking for a way to make a javascript widget that displays a data chart interactive by allowing users to change the theme? By using a dropdown box, users can select a new theme and instantly see it applied to the widget. Check out the code below: <sc ...

Tips for utilizing the !isNan method within a Jquery function to calculate the sum of values in both columns and rows within an HTML table

I am facing an issue with sum calculation in a table where some columns contain text strings and others contain numbers. When I perform the sum calculation, many NaN results are displayed. How can I remove these NaN values? I want to exclude Text string ...

What is the best way to programmatically disable a button in JavaScript or jQuery when all the checkboxes in a grid are either disabled or unchecked?

In my grid, one column contains checkboxes. I need to implement a feature where a button is disabled if all the checkboxes are unticked. How can I achieve this using JavaScript or jQuery? .Aspx File <asp:TemplateField HeaderText="Cancel SO Line Item"& ...

Achieving unique proportions with HTML5 video resizing

On my page, I plan to showcase a video specifically for desktop browsers. I have devised some code to determine whether the video is in landscape or portrait orientation. The challenge lies in deciding what to do based on this orientation: If the video is ...