Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
How to get the checked radio button in JS?

I have seen that this works for most of users, but for some reason it doesn't for me. I use Google Chrome.

radioBut = document.querySelector(".rad-design")
getColor = function(){
for (i=0; i<radioBut.length; i++){
    if (radioBut[i].checked){
        console.log(radioBut[i)   
    }
}

Html

                <form id = "rad">
                    <div class = "radioAll">
                        <label class="rad-label">
                            <input type="radio" class="rad-input"  name="colList">
                            <div class="rad-design"></div>
                        </label>
                        <label class="rad-label">
                            <input type="radio" class="rad-input"  name="colList">
                            <div class="rad-design"></div>
                        </label>
                    </div>       
                  </form>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The selector should be document.querySelectorAll to get inputs as array and you should target to .rad-input class which is the input and not .rad-design which is the label. Also you should use checked for the inputs to make the input checked, its not check. Also you cannot set checked to two inputs with same name. If thats done only the last input with that name will be checked.

Working Fiddle

const radioBut = document.querySelectorAll(".rad-input")
getColor = function () {
    for (i = 0; i < radioBut.length; i++) {
        if (radioBut[i].checked) {
            console.log(radioBut[i])
        }
    }
}
<form id="rad">
    <div class="radioAll">
        <label class="rad-label">
            <input type="radio" class="rad-input" checked name="colList">
            <div class="rad-design">One</div>
        </label>
        <label class="rad-label">
            <input type="radio" class="rad-input" name="colList">
            <div class="rad-design">Two</div>
        </label>
    </div>
    <button type="button" onclick="getColor()">getColor</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

document.querySelector returns just one element not an array/list, so in the for loop at i<radioBut.length radioBut.length is undefined, you need to use document.querySelectorAll() instead.

Also I noticed you have selected the div and not the input and you have a couple of syntax errors.

Maybe this can help you:

const radioBut = document.querySelectorAll(".rad-input")
const getColor = function(){
  for (let i=0; i<radioBut.length; i++){
      if (radioBut[i].checked){
          console.log(radioBut[i].value)   
      }
  }
}

console.log(getColor())
<form id = "rad">
    <div class = "radioAll">
        <label class="rad-label">
            <input type="radio" class="rad-input"  value='A' name="colList">
            <div class="rad-design"></div>
        </label>
        <label class="rad-label">
            <input type="radio" class="rad-input"  value='B' name="colList" checked>
            <div class="rad-design"></div>
        </label>
    </div>       
  </form>

Another options is to use the form element functionality

const form = document.getElementById('rad');

const getColor = function(){
   return form.colList.value;
}

console.log(getColor())
<form id = "rad">
    <div class = "radioAll">
        <label class="rad-label">
            <input type="radio" class="rad-input"  value='A' name="colList">
            <div class="rad-design"></div>
        </label>
        <label class="rad-label">
            <input type="radio" class="rad-input"  value='B' name="colList" checked>
            <div class="rad-design"></div>
        </label>
    </div>       
  </form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda