Utilize jQuery to set radio buttons based on their values on elements that are not checkboxradio objects (the buttons may have different behavior

My radio buttons are displaying differently when CSS is applied. I'm having trouble identifying the issue and how to resolve it properly. Using refresh isn't an option since they are not initialized as jQuery checkboxradio.

The radio buttons function as expected without CSS, but with CSS, the behavior may be different based on your knowledge but not desired in my case.

$("#btn0").click(function(e) {

  document.getElementById("rbtShareExp0").checked = true;

});
...

</fieldset>

Answer №1

This issue arises from the styling rules you have in place. The active state for the styled labels relies on

.switch-field input:checked + label
, which means that checking the first radio button will activate the second label. To fix this, simply rearrange the order of the labels so they come after the radio buttons, as demonstrated below:

$("#btn0").click(function(e) {

  document.getElementById("rbtShareExp0").checked = true;

});

$("#btn1").click(function(e) {

  document.getElementById("rbtShareExp1").checked = true;
});

$("#btn0a").click(function(e) {

  document.getElementById("rbtShareExp0_a").checked = true;
});

$("#btn1a").click(function(e) {

  document.getElementById("rbtShareExp1_a").checked = true;
});
.switch-field {
  font-family: Arial;
  overflow: hidden;
}
.switch-title {
  margin-bottom: 6px;
}
.switch-field input {
  display: none;
}
.switch-field label {
  float: left;
}
.switch-field label {
  display: inline-block;
  background-color: #D9EDF7;
  color: #1159a5;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.switch-field label:hover {
  cursor: pointer;
}
.switch-field input:checked + label {
  background-color: #A5DC86;
  -webkit-box-shadow: none;
  box-shadow: none;
}
.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<fieldset>
  <legend>with CSS:</legend>
  <div class="switch-field">
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0" value="0" /><label for="rbtShareExp0">No way</label>
    
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1" value="1" /><label for="rbtShareExp1">Yes, share it</label>
    
  </div>

  <input type='button' id='btn0' value='set to 0' />
  <input type='button' id='btn1' value='set to 1' />
</fieldset>


<fieldset>
  <legend>without CSS:</legend>

  <label for="rbtShareExp0">No way</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0_a" value="0" />
  <label for="rbtShareExp1">Yes, share it</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1_a" value="1" />
  </br>
  <input type='button' id='btn0a' value='set to 0' />
  <input type='button' id='btn1a' value='set to 1' />
</fieldset>

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

Delay loading background image until a specific time has passed, preventing website from fully loading

My website features a vibrant backgroundImage Slideshow that functions seamlessly. However, I am looking to reload the images of the slideshow after a specific time interval. The issue is that the website keeps loading endlessly. Once the website has com ...

What is the best way to extract an array from an AJAX call and process it as JSON data

I'm seeking assistance once more. My goal is to retrieve multiple rows from a table using AJAX in JSON format and populate textboxes with those values. Below is the code that I have so far. HTML: $.ajax({ /* Initiate AJAX */ typ ...

What are the steps to prevent a redirect?

I have two forms with different functionalities - one for submitting/adding and the other for selecting/redirecting. Currently, both forms are redirecting. How can I prevent the top form from redirecting after submission? <?php if($_POST){ / ...

Locate specific text within the content and apply an underline to it

Is there a way to locate specific text on my website and apply an underline to it? Suppose I have some text displayed and I wish to identify all instances of the word hello and underline them. Here is my attempt at the code: $( "body:contains('hell ...

How can I extract text from a website without retaining number, dot, and alphabet bullets in the list?

I am currently using Python 2.7.8 for a project involving a website with text displayed in an ordered list format using ol tags. I need to extract the specific text items listed below: Coffee Tea Milk This is an example of the HTML code used on my websit ...

``The text overlay feature on the image appears to be malfunctioning

I'm facing an issue with overlaying text on an image - the text is getting pushed underneath the image and not displaying as intended. I can't figure out what's causing this problem. Any assistance would be greatly appreciated. Below is the ...

Inline-block styling behaving incorrectly within span element

I am facing an issue with aligning a validation message inline with a textbox in my HTML code. Despite trying various options, the validation message appears just below the textbox instead of beside it. Below is the snippet of the code in question: HTML ...

What could be causing my HTML checkbox to not function properly on mobile devices?

Having an issue with my website where users are unable to click the checkbox on mobile devices. Despite searching online, I have not been able to find a solution to this problem. .jg-c-register_label { display: grid; } .jg-c-register_label-text { c ...

Change the current tab's destination to a different URL

During the development of a simple extension, I encountered an issue with relocating my current tab to a new location. The code snippet I tried using didn't yield the desired results: function redirect() { console.log("HELLO"); chrome.tabs.g ...

Close Tooltip when Ajax content is loaded

While working with jQuery tooltip, I encountered an issue. I have content loaded on a DIV element that is supposed to show a tooltip when I hover over an image map element within this DIV. Everything works fine until I click on one of the image map element ...

What is the method for incorporating various font sizes within tooltip content?

I need assistance with making the number of days (Difference_In_Days, Difference_In_DaysRev) appear in a larger font than the rest of the text within a tooltip. I attempted to include a <span style="font-size:25px;"> tag in the mess variable, but enc ...

Making buttons stand out when clicked using CSS styling

Is there a way to make the selection of multiple items responsive on a /getmatch page? Currently, I have no visual indicator when I select things: []]1 What can be done to enhance the responsiveness of selecting multiple items? Below is the code snippet ...

Attach the button to the input tag so that they are always connected

While working on the layout for my webpage, I encountered an issue with an input area and a textarea that I wanted to clone. Everything was functioning correctly, however, I wanted the "+" button to remain attached to the input tag even when the screen siz ...

Google Chrome - Automatically scroll to the bottom of the list when new elements are added in *ngFor loop

I have a basic webpage showcasing a list of videos. Towards the bottom of the page, there is a button labeled "Load more". When a user clicks on this button, an HTTP request is triggered to add more data to the existing array of videos. Here is a simplifi ...

Struggling to make this jQuery selector function properly

On my webpage, I have a recurring class named "a.add_mycrate". The script mentioned targets only the clicked instance (this) and interacts with it. Below is the JavaScript code: jQuery(document).ready(function(){ jQuery(document).on('click' ...

Incorporate a Font Awesome icon into a Bootstrap 4 callout

I am having issues adding an icon to the left side of a Bootstrap 4 callout. Currently, the icon is appearing above the H4 heading. I would like the icon to be positioned on the left side before the message. I have attempted using .p:last-child but it doe ...

Implementing initial state checks for Alpine.js checkboxes based on x-modal is not functioning properly

Upon loading alpinejs, for some reason all checkboxes become unchecked. It's perplexing and I can't figure out why. <div x-data="{ colors: [orange] }"> <input type="checkbox" value="red" x-model="co ...

Expand the height of the parent container as additional child elements are included

I'm facing a layout issue with a parent div and multiple child divs. Currently, the child divs are evenly spaced within the parent div, which works fine. However, I want the parent div to expand in height as more child divs are added, while keeping th ...

Transforming JSON information into selectable dropdown choices

Here is some data returned from an AJAX call that is now in JSON format: [{"PROCEDURE_ID":362183,"PROCEDURE_NAME":"Biopsy, Breast; Lumpectomy"}, {"PROCEDURE_ID":558975,"PROCEDURE_NAME":"Biopsy, Cervix"},{"PROCEDURE_ID":558977,"PROCEDURE_NAME":"Biopsy, int ...

Top method for passing props from child to parent React component

I am facing a challenge with passing data from child to parent components in React. I have an array that I need to save in my database, and I have set up a Parent component (AuthenticationPage.js) and a Child component (SignupForm.js) for this purpose. The ...