JavaScript in Survey123 without Survey123 Connect?

893
10
Jump to solution
10-12-2022 12:03 PM
RogerAsbury
Occasional Contributor

The Situation: I want to make two fields in a survey required, but only if the value in a dropdown menu equals one particular item from that menu.

It seems like adding javascript via Survey123 Connect comes with a lot of caveats, such as no access to the DOM and not being accessible in publicly viewable surveys. If I'm wrong on that I'd love to be educated. 🙂 

Is there a non-Survey123 Connect way to add javascript to a Survey123 survey? Or a way to do what I want without adding custom javascript? 

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
1 Solution

Accepted Solutions
DougBrowning
MVP Esteemed Contributor

You can just put a formula in the required column.  It defaults to the yes drop down but anything that evals to true will work.  You may get a green box yelling at you but it will go.

for example 

${someDropDownquestion} = "value"

or some people like to use this and must for select multiple

selected(${someDropDownquestion}, "value")

Hope that helps

View solution in original post

10 Replies
DougBrowning
MVP Esteemed Contributor

You can just put a formula in the required column.  It defaults to the yes drop down but anything that evals to true will work.  You may get a green box yelling at you but it will go.

for example 

${someDropDownquestion} = "value"

or some people like to use this and must for select multiple

selected(${someDropDownquestion}, "value")

Hope that helps

RogerAsbury
Occasional Contributor

Wouldn't this still need to be done via Survey123 Connect, and therefor not work for public surveys? Am I misinterpreting that restriction?

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
DougBrowning
MVP Esteemed Contributor

Sorry I have no idea.  Javascript is blocked from public (maybe changing?) but I did not think Connect was.  I hope not I was going to use that.

0 Kudos
IsmaelChivite
Esri Notable Contributor

I agree with @DougBrowning : Simply add an XLSForm expression to the required column and you will be set. No need for custom JS functions.

You can share your Survey123 Connect surveys publicly and use them from either the web or mobile apps.

RogerAsbury
Occasional Contributor

So I'm likely misinterpreting the limitation. 🙂 Wouldn't be the first time. I will give it a shot on a test survey and see what happens. Thanks much to the both of you.

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
RogerAsbury
Occasional Contributor

Ok, on a test survey, I created a number of questions, primarily "Type", "Email" and "Phone". Type has a number of options with radio buttons. I only want Email and Phone to be required if the Type is Auto Accident. So in XLSForms, under the "Required" column for Email and Phone, I've placed:

selected(${type}, "traffic_accident")

I've also tried the ${someDropDownquestion} = "value" format. Yet when I update and publish the form, these two questions still appear to be skippable when I select traffic accident as the type. Am I placing this in the proper column?

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
DougBrowning
MVP Esteemed Contributor

At least in Connect type is a reserved word and cannot be used.  Not sure on web.

Also make sure that the name value is what you use in the selected part and now the label.

0 Kudos
RogerAsbury
Occasional Contributor

Ok, with that prodding, I've got the validation working. Email and Phone are now required only when Auto Accident is chosen. However, this has lead to another, possibly more annoying problem...

noeditforyou.png

 

Now that the validation is working, I can no longer edit the survey in Survey123. Is there no happy medium here? I can't possibly be the only person who thinks designing a survey via Excel spreadsheet is crazy kludgy...

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
DougBrowning
MVP Esteemed Contributor

Nope it is one or the other.  This was just posted today take a look.   Once you get used to the excel it is much more powerful but for sure more of a programmers interface.

0 Kudos