0

I need to create a js that can select radio buttons based on the values of radio buttons the user clicks. The conditions are the following:

Question 39: Yes must be selected if Yes is selected for one of questions 41 - 45. Otherwise, it's No.

Question 40: Yes must be selected if Yes is selected for ALL of questions 41-45. Otherwise it's No.

This is what I have:

function CBR() {
  for ($i = 41; $i < 46; $i++) {
    if ($('input[name=' + $i + '][id=Yes]').checked == true) {
        $('input[name=39][id=Yes]').click();
    }
  }
}
<td width="66px">
  <input type="radio" id="Yes" name="<?php echo $question['id']; ?>" value="1" onClick="CBR()">Yes</td>

<td width="66px">
  <input type="radio" id="No" name="<?php echo $question['id']; ?>" value="0">No</td>

<td width="66px">
  <input type="radio" id="NA" name="<?php echo $question['id']; ?>" value="1" checked="true">N/A</td>

What I know the code does as of now is that it checks if any one of 41-45 is selected yes, and if so, it selects Yes for 39. I was just testing to see if this worked and it doesn't look like it, so I can't really move forward. Can anyone help?

To clarify: I have used document.getElementbyID, but if I do, I can't refer to the question number in the js, which is the "name" in html.

some1
  • 5
  • 5

1 Answers1

0

You'll find the way of checking if a radio button is checked here (StackOverflow question).

Here is a snippet of how to check a radio button with JavaScript:

document.getElementById(elementID).checked = true;
Community
  • 1
  • 1
Lionel Renaux
  • 152
  • 1
  • 14
  • The getElementbyID lets me select only one element at a time, so I don't think it makes sense to say "if (document.getElementbyID("Yes").checked == true" for every question. I've tried Tags and querySelectorAll, both of which don't work either. – some1 Feb 22 '16 at 13:09
  • @some1 Look on the link I've provided in my answer to see how to verify if a radio button is checked: http://stackoverflow.com/questions/9305248/javascript-check-if-radio-button-was-checked My snippet with the `document.getElementById` is only there to show you how to make a radio button checked. – Lionel Renaux Feb 22 '16 at 13:54