Checking radio boxes are selected using Javascript

Both removed, thank you.
still doesn’t work though

function checks() {
  
  var radioSets = document.querySelectorAll(".answers-div");
  forEach (i = 0; i < radioSets.length; i++) {
    var radioButtons = [ ...radioSets.querySelectorAll('input[type=radio]') ];
    var newVar = false; 
  
    forEach (var i = 0; i < radioButtons.length; i++) {
      if (radioButtons[i].checked) {  
        newVar = true; 
        break;
      }
    }
  if (newVar == true) { 
    radioButtons.class.remove(".message_visible");
  }
  if else (newVar == false) { 
    radioButtons.class.add(".message_visible"); 
  }
}

Research how to use forEach syntax or better yet, just use a for loop syntax.

I havn’t ever used a forEach loop you would be correct… so I’ve replaced with the for loop and tweaked the else statement at the bottom. Why do I still feel like its not targeting the <p> element or is it?

function checks() {
  
  var radioSets = document.querySelectorAll(".answers-div");
  for (var i = 0; i < radioSets.length; i++) {
    var radioButtons = [ ...radioSets.querySelectorAll('input[type=radio]') ];
    var newVar = false; 
  
    for (var i = 0; i < radioButtons.length; i++) {
      if (radioButtons[i].checked) {  
        newVar = true; 
        break;
      }
    }
  if (newVar == true) { 
    radioButtons.class.remove(".message_visible");
  }
  else {
    radioButtons.class.add(".message_visible"); 
  }
}

radioButtons is an array and not the applicable p element. You should target the radio button set and add/remove the class from the class list (research this part).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.