Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Document.mainform['[QName'].value Not Working

I've got an older version of SSI Web and when I try to use document.mainform["[QNAME"].value, I get an undefined error when pulling it in in the custom verification, to wit the code is:

var numkids = document.mainform["Q75"].value;

numkids ends up being undefined.

The question is:

How many dependent children live in your household? [Response list is 0 to 5+]

document.getElementById works when I use freeform but doesn't here.
asked Jun 19, 2018 by Corey1975 Bronze (740 points)
Is this a free format question?  If so, what HTML are you using in it?
It's not a freeform question. It's a standard Select question.  The manual says to use document.mainform["QNAME"].value for javascript verification but that doesn't work nor does getElementById (which works fine in freeform).

1 Answer

0 votes
If you take a look at the HTML of your survey page, you will likely see that each radio input has the same name (likely "Q75"), but probably different IDs (likely "Q75_1," "Q75_2," etc.).

document.getElementById is great for grabbing a single element by its ID, but if we want to know which radio input is selected, we probably need to grab all radio inputs with document.getElementsByName.  Once we have all inputs, we can iterate through them and see which one is checked.  Please try this:

var selected = -1;
var radios = document.getElementsByName('Q75');
for (var i = 0; i < radios.length; i++) {
    if (radios[i].checked) {
        selected = radios[i].value;
        break;
    }
}


After running, "selected" should be set to the value of which radio button was selected, or -1 if none were selected.
answered Jun 19, 2018 by Zachary Platinum Sawtooth Software, Inc. (120,175 points)
...