S.P. Soni
S.P. Soni

Reputation: 39

How to check the array of radio buttons is checked or not?

Following is the html content and I want to check whether one of the radio button is checked or not?

<div class="col-sm-4 rating">
                                    <input type="hidden" name="questionId[]" value="4"/>
                                    <input type="radio" class="career_ratings" id="career_star31" name="career_rating_answer[3]" value="1" />
                                    <label  for="career_star31" title=""></label>

                                    <input type="radio" class="career_ratings" id="career_star32" name="career_rating_answer[3]" value="2"/>
                                    <label  for="career_star32" title=""></label>

                                    <input type="radio" class="career_ratings" id="career_star33" name="career_rating_answer[3]" value="3"/>
                                    <label  for="career_star33" title=""></label>

                                    <input type="radio" class="career_ratings" id="career_star34" name="career_rating_answer[3]" value="4"/>
                                    <label  for="career_star34" title=""></label>
                                </div>

Upvotes: 0

Views: 1280

Answers (1)

manoj kumar c.a
manoj kumar c.a

Reputation: 146

https://plnkr.co/edit/vlYNcBeEUkP0wLtcbqtV?p=preview

<!DOCTYPE html>
<html>

<body>

  <form>
    <div class="col-sm-4 rating">
      <input type="hidden" name="questionId[]" value="4" />
      <input type="radio" class="career_ratings" id="career_star31" name="career_rating_answer[3]" value="1" />
      <label for="career_star31" title="1">1</label>

      <input type="radio" class="career_ratings" id="career_star32" name="career_rating_answer[3]" value="2" />
      <label for="career_star32" title="2">2</label>

      <input type="radio" class="career_ratings" id="career_star33" name="career_rating_answer[3]" value="3" />
      <label for="career_star33" title="3">3</label>

      <input type="radio" class="career_ratings" id="career_star34" name="career_rating_answer[3]" value="4" />
      <label for="career_star34" title="4">4</label>
    </div>
    <button onclick="check()">Try it</button>

  </form>

  <script>
    function check() {
      var radio = document.getElementsByClassName('career_ratings');
      var x = false;
      for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
          x = true;
        }
      }
      alert(x)
    }
  </script>

</body>

</html>

Upvotes: 1

Related Questions