cambiar eel color de fondo de un button radio in javascript

To change the background color of a radio button in JavaScript, you need to select the radio button element and set its background-color property using the style object. Here's an example:

<!-- HTML markup -->
<input type="radio" id="my-radio-button" name="my-radio-buttons" value="option-1">
<label for="my-radio-button">Option 1</label>
150 chars
4 lines
/* CSS styling */
#my-radio-button {
  /* your radio button styles */
}

#my-radio-button:checked + label {
  /* your label styles when radio button is selected */
}
166 chars
9 lines
index.tsx
// JavaScript code
const myRadioButton = document.querySelector('#my-radio-button');

myRadioButton.addEventListener('change', () => {
  if (myRadioButton.checked) {
    myRadioButton.parentElement.style.background = '#f0f0f0';
  } else {
    myRadioButton.parentElement.style.background = 'none';
  }
});
306 chars
11 lines

In the example above, we add an event listener to the radio button that listens for the 'change' event. When the radio button is checked, we set its parent element's background color to '#f0f0f0'. When it's unchecked, we set the background to 'none'.

Note that in the CSS code, we select the label element adjacent to the radio button using the '+' selector. This allows us to target the label when the radio button is checked, and style it accordingly.

related categories

gistlibby LogSnag