Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
How to get checked values of all other Radio Buttons

I have created this code example for selecting items from radio button among the given data. In this, when we click on any input, its value is console logged, but I am not able to figure out on how to console log other fieldset selected values as well, so for eg:- If I select, Blue it will console log Blue, but I want it to console log something like: - Blue and the rest are S. Other checked values from another position of data should also be console logged. Any help will be greatly appreciated.

const something = document.getElementById('something');

const data = [{
  name: 'Color',
  values: ['Blue', 'Red', 'Black']
}, {
  name: 'size',
  values: ['S', 'M', 'L']
}]

const variOptions1 = data[0].values[0];
const variOptions2 = data[1].values[0];
something.innerHTML = data.map((modalVari, indexVariposition) => {
 return   `
<fieldset class="modal-variant-form">
    <legend class="modal-variant-title">${modalVari.name}:</legend>
   
    ${modalVari.values.map((varival, variIndx) => (`
               <input type="radio" id="variant-${indexVariposition}-${variIndx}" name="${modalVari.name}" value="${varival}"${variOptions1 == varival || variOptions2 == varival  ? 'checked' : ''}>
               <label for="variant-${indexVariposition}-${variIndx}">
                   ${varival}
               </label>
           `)).join("")}
   
</fieldset>
`
}).join('');


for (var h = 0; h < data.length; h++) {
  const fieldFirst = data[h].name;
  const radioButtons = document.querySelectorAll(`input[name="${fieldFirst}"]`);
  for (const radioButton of radioButtons) {
    radioButton.addEventListener('change', showSelected);
  }

  function showSelected(e) {

    if (this.checked) {
      console.log(`You selected ${this.value}`);

    }
  }
}
<div id="something"></div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use

document.getElementsByName('color').forEach(
 function(elem) {
  if(!elem.checked) {
   //your code if the radio button not checked
    }
   }
  )

In this case, you should set all of the radio buttons that you want to see name attribute to color.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simplify your script and make it loop over all radio buttons. Only when a button is .checked its value is printed:

const something = document.getElementById('something');

const data = [{
  name: 'Color',
  values: ['Blue', 'Red', 'Black']
}, {
  name: 'size',
  values: ['S', 'M', 'L']
}]

something.innerHTML = data.map((modalVari, indexVariposition) => {
  return `
    <fieldset class="modal-variant-form">
        <legend class="modal-variant-title">${modalVari.name}:</legend>
       
        ${modalVari.values.map((varival, variIndx) => (`
                   <input type="radio" id="variant-${indexVariposition}-${variIndx}" name="${modalVari.name}" value="${varival}">
                   <label for="variant-${indexVariposition}-${variIndx}">
                       ${varival}
                   </label>
               `)).join("")}
       
    </fieldset>
    `
}).join('');

const radioButtons=[...something.querySelectorAll("input[type=radio]")];

something.addEventListener("click",ev=> {
 if (ev.target.type!=="radio") return;
 console.log(Object.fromEntries(radioButtons.filter(rb=>rb.checked)
  .map(rb=>[rb.name,rb.value])))
})
<div id="something"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda