Is there a way to apply a class to the main div once the checkbox has been clicked?

Whenever the checkbox is checked, it should add a class to the main div. Additionally, if I try to check another checkbox, the previously checked one should have its class removed and the new one should have the class added.

Here is the code snippet for reference:

$(".img-rdo :checkbox").change(function() {
  $this.closest(".img-rdo").toggleClass('checked', this.checked);
})

$('.img-rdo .imgRdo').change(function() {
  $(this).closest('.img-rdo').find(':checkbox').prop('checked', this.value != 0).change();
});
.img-rdo {
  background: yellow;
  margin: 10px;
}

.img-rdo.checked {
  background: green;
  margin: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="img-rdo" nk-rdo-style="1">
  <input type="radio" id="img-rdo2" name="img-sample-1" class="imgRdo">
  <label class="imgRdo-lbl" for="img-rdo2">
    <div class="rdo-txt">Radio Button with image design component</div>
  </label>
</div>

<div class="img-rdo">
  <input type="radio" id="img-rdo3" name="img-sample-1" class="imgRdo">
  <label class="imgRdo-lbl" for="img-rdo3">
    <div class="rdo-txt">Radio Image</div>
    </label>
</div>

<div class="img-rdo">
  <input type="radio" id="img-rdo4" name="img-sample-1" class="imgRdo">
  <label class="imgRdo-lbl" for="img-rdo4">
    <div class="rdo-txt">Radio Image</div>
  </label>
</div>

Answer №1

To make the necessary changes, remember to update the container class and avoid wrapping a div within a label.

$(".imgRdo").change(function() {
  $(".img-rdo").removeClass('checked');
  $(this).closest('.img-rdo').toggleClass('checked', this.checked);
})
.img-rdo {
  background: yellow;
  margin: 10px;
}

.img-rdo.checked {
  background-color: green;
  margin: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="img-rdo" nk-rdo-style="1">
  <label class="imgRdo-lbl" for="img-rdo2">
    <input type="radio" id="img-rdo2" name="img-sample-1" class="imgRdo">
    <span class="rdo-txt">Radio Button with image design component</span>
  </label>
</div>

<div class="img-rdo">
  <label class="imgRdo-lbl" for="img-rdo3">
  <input type="radio" id="img-rdo3" name="img-sample-1" class="imgRdo">
    <span class="rdo-txt">Radio Image</span>
    </label>
</div>

<div class="img-rdo">
  <label class="imgRdo-lbl" for="img-rdo4">
  <input type="radio" id="img-rdo4" name="img-sample-1" class="imgRdo">
    <span class="rdo-txt">Radio Image</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

"Troublesome issue with Bootstrap push and pull functionality not functioning correctly

I am currently experiencing issues with Bootstrap push and pull functionality. While the mobile view appears to be working correctly, the web view is shifted to the left. Any assistance on this matter would be greatly appreciated. Below is the code that i ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

Using JavaScript to inject PHP variables into multiple <span> elements

I have been searching for a similar query without any luck. Currently, I am attempting to display the number of Twitter/Facebook/RSS followers within a <span>....</span> in my sidebar. To retrieve the follower counts, I am using some PHP scrip ...

I'm confused why this code is functioning in JSFiddle but not on my HTML webpage

For the first time, I am encountering this issue. I am currently attempting to integrate this code into my application http://jsfiddle.net/TC6Gr/119/ My attempts include: Pasting all the jsfiddle code in a new page without my code, but it doesn't w ...

How to temporarily toggle an event on and off using jQuery

Is there a way to control the activation and deactivation of a jQuery event? I currently have this functioning code that runs when the page is ready: $(".panzoom").panzoom({ $zoomIn: $(".zoom-in"), $zoomOut: $(".zoom-out"), $zoomRange: $(".zoo ...

Issues with scaling background videos in HTML5

I'm having trouble making a video scale properly with the browser window while still covering the entire area. Does anyone know why my current approach isn't working? HTML: <div class="bgVideoWrap"> <video id="bgVideo" loop="true" aut ...

Dealing with Mouse-Operated Data Deletion in a TextBox

I have a unique requirement that involves displaying a button labeled Set only when the text in two separate Text input fields matches. If the text in both fields matches, I am successfully able to control the visibility of the Set Button. However, I enc ...

Should you include the dollar sign in a Vue HTML variable or not?

I’m a bit confused about whether or not I should include $ when using a Vue HTML variable: new Vue({ data: { a: "myData" } }); Do I need to use: <h1>My value is {{ a }}</h1> or <h1>My value is {{ $a }}</h1> What ...

Is there a way to soften the repetitive elements in the background image?

Is there a method to blur the part of a background image that is repeated, such that if it is positioned in the center, the sides of the image appear blurry as well? ...

Attempting to migrate a HTML website to WordPress has proven to be a challenge as only the content is visible, with the CSS failing to load properly

I'm looking to convert an HTML site into a WordPress site while keeping the same theme intact. Even after adding 'wp_enqueue_style' to the functions.php file, the CSS file is not being recognized by WordPress. functions.php <?php f ...

Create visually dynamic charts using either JQuery or CSS based on the values stored in Div elements on the webpage

Can a Jquery or CSS chart be constructed using the values in a set of Divs on a page? For example, if my page contains the following: Value One = <div id="q1">21</div> Value Two = <div id="q2">40</div> Can a bar chart be created ...

Customize the design of a React Material-UI select dropdown by overriding

I am currently utilizing the React MUI Select Component for my forms, specifically for language selection. My issue arises when I need a different style for the dropdown menu of the language Select component. Overriding the relevant class would apply this ...

Images for navigating forward and backward in a jQuery datepicker

I'm experimenting with setting custom images for the previous and next month buttons. I attempted to utilize the prevText / nextText options of the datepicker in order to include a span with my own CSS class definition. However, this resulted in the c ...

The minimum and maximum validation functions are triggered when I am not utilizing array controls, but they do not seem to work when I use array controls

Take a look at the stack blitz example where min and max validation is triggered: https://stackblitz.com/edit/angular-mat-form-field-icrmfw However, in the following stack blitz with an array of the same controls, the validation does not seem to be worki ...

Utilizing a webkit transition to conceal a div element by setting its display property to "none"

I'm currently working with code that looks something like this: <style> #submenu { background-color: #eee; height:200px; width:400px; opacity: 1; -webkit-transition: all 1s ease-in-out; } .doSomething { ...

How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens. For more information on the componen ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; import { connect } from 'react ...

hover-triggered keyframe animation

Recently, I've been experimenting with CSS keyframe animation using the code below: .pulse-animation { margin: 50px; display: block; width: 52px; height: 52px; border-radius: 50%; background: #ff8717; cursor: pointer; animation: pul ...

Child element casting shadow over parent element

I am currently using box shadow for both the parent (.map) and child (.toggle-button): .map { position: fixed; top: 20px; right: 0; width: 280px; height: 280px; z-index: 9999; box-shadow: 0px 1px 6px 0px rgba(0,0,0,0.3); } .map ...

There seems to be an issue with the CSS file linking properly within an Express application

Every time I run my app.js file, the index.html file is displayed. However, when I inspect the page, I notice that the CSS changes are not taking effect. Strangely, if I open the HTML file using a live server, the CSS changes are visible. Can someone exp ...