Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!