[Solved] Custom Scripting Access to Radio Button Comments and Set Comment as Required Based on Radio Button Answer


I have a survey where there is a number of “audit” questions for a retail store visit.  Each audit question have four potential answers:

  • In Compliance
  • Deviation
  • Critical Deviation
  • Exempt

There is also a comment box attached to each question that is not required.

What I want to do is the following via custom scripting:

  1. Display all comments on the final summary page (not the page post clicking the submit button, but just the last page of the survey) where the comment entered was rated a Deviation or Critical Deviation.  I know how to access the answers via scripting but I can not find an ID number to reference the comment text box.
  2. I would like to set the requirement to enter a comment if the auditor answers the question as Deviation or Critical Deviation.

Does anyone know how to do either of these two processes.

Thanks in advance.

waellotfy77 answered

    Best answer


    Hi Jon,

    I order to display the comments for questions rated “Deviation” or “Critical Deviation” I would grab the survey’s page map and iterate over it using a few different sgapi functions to access their values and comments if appropriate. For example…

    // get the map of your survey
    %%pagemap = sgapiPageMap();
    // iterating over pages of the pagemap
    foreach(%%pagemap as %%page_id => %%questions_on_page_arr){

    // iterating over question types
    foreach(%%questions_on_page_arr as %%question_id => %%question_type){

    //if it’s a radio button
    if(%%question_type == ‘RADIO’){

    // get the rating
    %%val = sgapiGetValue(%%question_id);

    // if the rating was Deviation or Critical Deviation, get it’s comment
    if(%%val == ‘Deviation’ || %%val == ‘Critical Deviation’) {

    // get the comment form runtime properties…
    %%comment = sgapiGetQuestionRuntimeProperty(%%question_id, ‘Comment’);

    // right now %%comment is an obj, we need it as array, so cast it as one…
    %%comment = (array) %%comment;

    // there is another nested obj, so cast it as an array too…
    %%comment = (array) %%comment[’atoms’][0];

    // finally if there is a comment, get it and print it…
    if(%%comment[’raw_value’] != ”) {
    %%output .= sgapiPrint_R(%%comment[’raw_value’]);
    %%output .= ‘<br/>’;

    As for setting the comment fields to be required, you could potentially use sgapiSetQuestionRuntimeProperty. However, that would require a large amount of page jumping and headache so I would use a JS action instead. With JS you can evaluate DOM events and trigger errors dynamically.

    Here is a rough start…


    // add listener to page submit event

    // select all radio buttons and iterate over them

    // remove the error message if present

    var value = $(this).find(‘input:checked’).attr(‘title’);

    // if they answered Deviation or Critical Deviation run custom validation…
    if(value == ‘Deviation’ || value == ‘Critical Deviation’) {

    // check to make sure there is a comment
    var comment = $(this).find(‘textarea’).val();

    if(comment == ”) {

    //if blank throw an error message
    $(this).find(‘.sg-comment-title’).append(‘<div class=”sg-error-message sg-error-display”>This comment field is required.</div>’);

    // stop page from submitting



    The JS action would need to be on every page and it would target every question on that page that has is a radio button. To get around this (if you need to) you could add a unique class to all the questions you want targeted. I hope this helps!

    joshwa answered
      • Josh, why are your solutions so much more elegant than the ones that I code? 😉

      • Josh, I don’t know why this is not working. I have added the javascript to page two of the survey as a javascript action, but it is not firing. I don’t know if that is being caused by some other scripting but I don’t have anything else on Page 2. Let me know if you can see any reason the javascript validation code is not firing?

      • Josh, I figured out the problem. When I copied your javascript code into the action firefox converted the ‘ ‘ to a ” and so it was failing. I ran this against the javacript console in Chrome and it found the error, so now it is working just perfectly.

      • Josh, your recommended solution is working great, thank you, but the client is wondering how we can focus on the question that triggered the validation error after the e.preventDefault(); function runs.

        Right now the page is not submitted because of the error but there is no indication to the respondent that there is a comment that is required. Can you tell me how I can bring focus to the specific question that triggered the error?

        Thoughts and suggestions would be greatly appreciated.

      • In the demo, I append an error-message above the required comment field. That should be plug-and-play. Is it not working for you?

      • Josh the script is working to stop the respondent from moving forward to the next page and it appends the error message to the question that needs to be addressed. The issue is that when the page is submitted and the script triggers the e.preventDefault(); function so the page does not progress to the next page but refreshes as is, it does not create any user indication on why the page did not progress nor does it create focus on the question that triggered the error (where the error message is displayed).

        I was hoping that there was an existing sg function for creating focus on a question that has an error that needs to be addressed. The standard question validation within SG does this, I was just hoping to be able to incorporate it into this java script.

      • Basically trying to do something like this (http://stackoverflow.com/questions/3656467/is-it-possible-to-focus-on-a-div-using-javascript-focus-function) but to use the window.location.hash = ‘#location’; I need to know what id to use instead of #location. This is why I was hoping there was an existing SG function I could include in the validation script you gave me.

      • i was able to fix this problem using the following function within the script above:

        // Bring error message into focus
        document.getElementsByClassName(“sg-error-message sg-error-display”)[0].scrollIntoView(true);


      i need help to modify this  script to apply for continuous sum question to force the respondent if they put a value in field (others ) to specify others in the comment 

      waellotfy77 answered

        I found this article that I had not seen before and I think it addresses the point about accessing the comments to pull a summary of them.


        My question about trying to make the comment box required if one of the answers is selected but not the others is still unanswered.

        Jon Erickson answered

          There are over 140 questions to this retail store audit process which is why I am hoping that there is some type of question ID or option ID as it will be a lot of work trying to add all those question (even with the question copy function once the styling is done to the first one).  I know your suggested solution will work, it is just not too practical in this case.  It would also address my second point but again I hope I can find some type of ID that I can use in the custom scripting.  Thanks for the response.

          Jon Erickson answered

            I am not an expert in script but you could make the comments actually another questions (essay type) and add logic to it. Then use the script to hide the title, script below and then use hidetitle in the css field. It will look like one question with logic


            .hidetitle .sg-question-title {display:none;}

            Mary Mawhirter answered


              Question stats

              • Active
              • Views36894 times
              • Answers5 answers
              • Followers1 follower