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

The button that is disabled can still be triggered using the ".click()" function

When programmatically triggering a disabled button with an "onclick" function, the function is still fired. See example below: <div> <input type="button" onclick="save()" id="saveButton" value="save" disabled="disabled" /> <input ...

Utilizing the power of Datatables through AJAX calls with HTML Forms

Hello, I am currently working on incorporating djangorestframework-datatables with the datatables' JQuery plugin. My task involves loading a large table (consisting of approximately 15000 entries, paginated) with the serverSide option enabled. Enabli ...

Show an Array of Nested JSON API information on an HTML page using jQuery/JavaScript

Recently, I've delved into the world of jQuery, JavaScript, and AJAX while experimenting with an API that I designed. The backend result I received looks like this: [ { "id": 1, "choice_text": "They work on List View generally", ...

Setting the default <a-sky> in Aframe: A step-by-step guide

There was a fascinating projection I witnessed where two images were displayed in the sky. [https://codepen.io/captDaylight/full/PNaVmR/][code] Upon opening it, you are greeted with two spheres and a default white background. As you move your cursor over ...

When viewed on a mobile device, the entire HTML document is resized to occupy less than half of the

Whenever I adjust the size of my webpage on different devices or mobile phones, I notice that the <div> tag appears to be only half (or even less) the width of the page. This gap seems to increase as the window size decreases. Refer to the screenshot ...

When setting the height to auto in Dreamweaver for a background image, no content will be displayed

I have been searching through several questions on this platform today in the hopes of finding an answer to my query, but unfortunately, I haven't had any luck so far. That's why I'm turning to you all for help. My current goal is to transi ...

Warning: Proceed with caution - extracting the value from a text area

<%@ Page Language="C#" ValidateRequest="false" MasterPageFile="~/PersonPage/ConfigPersonalPage.master" AutoEventWireup="true" CodeFile="ConfighPropertise.aspx.cs" Inherits="PersonPage_ConfighPropertise" %> <textarea style="width:850 ...

When the checkbox is not selected, the content on the page will revert back to its original state

Is there a way to dynamically change content on a page when a checkbox is checked, and revert it back when the checkbox is unchecked? I don't want to manually set each element's value to default in JavaScript and CSS. <div class="switch&q ...

A step-by-step guide on serializing a model and sending it to an MVC controller through an Ajax

I need to transfer the model of my webpage to my controller for processing. Once the information has been processed, I want to update the div with the id "savedText" to show "Billing Information saved successfully." Here's what my view looks like: ...

Mobile devices are unable to properly implement the Jquery show/hide feature

Currently, I am working on a small Russian project and facing some challenges with the mobile version of my website: php8098.github.io/breakfast. The issue lies within the first slider where the play button should trigger a modal window to open. I urgentl ...

Retrieving dynamic id values from input fields using jQuery

Is there a way to retrieve the values from input fields with changing IDs in a jQuery function? <input type="text" id="a_8" name="a_8" value="12"> <input type="text" id="b_8" name="b_8" value="22"> <button type="button" class="btn btn-suc ...

Do designers often manually adjust elements using media queries when working with Bootstrap?

Is it common practice to directly invoke media queries in order to manually adjust the layout of elements when using Bootstrap? In other words, if I have a custom element that I need to display or position in a specific way, is it acceptable to call the m ...

Tips on placing a white background behind fa-3x (Font-awesome) icons

I am facing challenges while trying to customize the background behind my font-awesome icons. My goal is to create a white background that does not extend beyond the actual icon itself. Currently, the result looks like the image below: https://i.sstatic. ...

Send Symfony2 form data via AJAX

When trying to render a form with AJAX and update existing values, I am facing an issue. Even after using the preventDefault method in my script to stop form submission, the form is still submitting. Here's the snippet of my script: $('#edit-co ...

iOS CORS ajax request is stuck at state 0 during processing

I am facing an issue with making a CORS login AJAX call from my iPhone using $.ajax. The request fails and reaches the fail callback, showing the jqXHR object state as: { readyState: 0, status: 0, statusText: "error" } Strangely, on my PC the request ...

Failing to retrieve data from Ajax response

When handling requests in a servlet, the following code snippet processes the request received: Gson gson = new Gson(); JsonObject myObj = new JsonObject(); LoginBean loginInfo = getInfo(userId,userPwd); JsonElement loginObj = gson.toJsonTree(loginInfo) ...

What is the best way to intelligently cache specific segments of a page in PHP?

Issue: I am working on a website with over 50,000 pages, the majority of which are only updated once at the time of creation. I am in the process of implementing caching for these pages since they do not require frequent content changes. However, I am fa ...

Struggling to devise a for loop for a JavaScript-generated list

I'm attempting to loop the content of an H1 element through a list 10 times. I seem to have made a mistake in my code and would appreciate any guidance. var headOne = document.createElement("H1"); headOne.textContent = "Hello World"; document.body. ...

What steps can I take to ensure that the elements are in the same row instead of being displayed in three separate rows?

(I'm a beginner in web development and need some help) Is there a way to align elements into the same row instead of stacking them up in separate rows? I'm working on creating a header bar similar to the one on the Naive UI Documentation Website. ...

locomotory mesh decentralized sorting

I am attempting to implement in-browser sorting for my flexigrid. Currently, the grid is displaying data from a static XML file exactly how I want it, but the table itself does not sort because it is working off of local data. While researching solutions, ...