Have an idea?

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

JavaScript for Free Format Question

Is there a chance to install a JavaScript for a FreeFormat Question? I was trying to get the length of a numeric input of a free format question (Time1).

if (String.valueOf(SSI_GetValue("Time1")).length() > 5)
{
    strErrorMessage = "Check your input.";
}

Also when I try to display the free format question on one of the next pages by using [% Time1%], this is also not working.

Thanks in advance.
asked Nov 7, 2022 by JoshuaS (295 points)
Free format questions do not include a survey variable whose name is the same as the question.  Instead, you can define your own variables for the free format and the resulting name will be "[QUESTION NAME]_[VARIABLE NAME]."  So if your free format is named "Time1" and you add a variable called "value", you should be referencing "Time1_value" in your codes.

I don't know if String.valueOf is going to get us what we want.  I'd try something like this (using the question name and variable name like above):

if ($('#Time1_value').val().length > 5)


But, if we can step back a bit, are you simply trying to apply a max response value to this field?
Thanks a lot for your reply. No what I acutally wanted to do is to make sure that if participants answer one question in a row they also reply to the other two questions in the row.

So in the first column I have a numeric value, in the second a select dropdown (default value "") and in the last column I have a numeric question again. So that was my code however it is somehow not working.

```
if ((($('#Question_Column1').val().length > 0) || ($('#Question_Column2').val() != "")  || ($('#Question_Column3').val().length > 0)) &&(($('#Question_Column1').val().length == 0) || ($('#Question_Column2').val() == "")  || ($('#Question_Column3').val().length == 0)))
{
    strErrorMessage = "Check your input.";
}
```
I've created a free format question with these variables as you have specified and added this code as custom JavaScript verification.  When I test the survey, I find that I am able to submit the page when I answer all or none of the questions, but not when I answer only one or two.  Is there something else I need to do to reproduce the problem?
Thanks for your help. Somehow the submit button is not working when I try it.

Maybe the way I coded the columns helps:

CLASS="numeric_input" TYPE="text" NAME="Question_Column1" ID="Question_Column1"
<SELECT NAME="Question_Column2" ID="Question_Column2"
<OPTION VALUE="">Please select</OPTION>
<OPTION VALUE="1">Option1</OPTION>
<OPTION VALUE="1">Option2</OPTION>
<INPUT TYPE="tel" CLASS="numeric_input" NAME="Question_Column3" ID="Question_Column3"
This seems to be an incomplete excerpt, but the first thing I notice is that there doesn't appear to be a closing "</select>" tag.  Is that the case with your HTML?
Yeah, sorry for this. It is storing the data fine, however, when I include the JavaScript I simply can not submit. Here is the full excerpt

<INPUT CLASS="numeric_input" TYPE="text" NAME="Question_Column1" ID="Question_Column1"SIZE="4" MAXLENGTH="4">
<SELECT NAME="Question_Column2" ID="Question_Column2">
<OPTION VALUE="">Please select</OPTION>
<OPTION VALUE="1">Option1</OPTION>
<OPTION VALUE="2">Option2</OPTION>
<OPTION VALUE="3">Option3</OPTION>
<OPTION VALUE="4">Option4</OPTION>
<OPTION VALUE="5">Option5</OPTION>
<OPTION VALUE="6">Option6</OPTION>
</SELECT>
<INPUT TYPE="tel" CLASS="numeric_input" NAME="Question_Column3" ID="Question_Column3" SIZE="4" MAXLENGTH="4">
Your opening "<select>" tag appears to be missing its ">" character that should appear before the "<option>" elements.
Sorry, I edited, however, I had it in the main script. Without JavaScript everything is working fine. Just the JavaScript is not working the way I want to
I've tried the HTML and JS you've provided, but am able to submit the page without responding to anything.  Could you share your .ssi with our support team so we can look at exactly what is happening?
Zachary, I find a way to fix it, it was actually a missing ID in one of the rows. Sorry, and thanks a lot for your help.

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...