Have an idea?

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

Trouble retrieving inputs to keep note page displayed if there is text on page

Hi everyone,

I am working on a survey with a note page after every question. Now when F10 or the note button is pressed, the notes come up. After moving to another question and coming back, the notes are again hidden.

What I am trying to do is get the notes from Q1 for example to remain displayed after skipping back to Q1 from Q2 if there is anything written in them, but to stay hidden if they are empty.

<style>
#[% QuestionName() %]_div {display:none}
</style> 

<script>
$(document).keydown(checkNote);
function checkNote(f){
    f = f || window.event;
    if (f.keyCode == '121' || f.keyCode == '35'){
        f.preventDefault(f);
        $("#[% QuestionName() %]_div").css('display', 'block');
        }
}

var $input1;
$('#[% QuestionName() %]').focus(function() { $input1 = $(this);
});

$(document).keydown(checkNote);
function displaynote (d){
    d = d || window.event;
    if  ($input1.val().length != 0) {
        d.preventDefault(d);
        $("#[% QuestionName() %]_div").css('display', 'block');  
        }
      }
<script>



This is the code that I have been using to try to achieve that. However even when I don't use jquery inputs and have tried something simpler,

function displaynote(d){
    d = d || window.event;
    if  ( SSI_GetValue("e7011mnote") == 1) {
        d.preventDefault(d);
        $("#[% QuestionName() %]_div").css('display', 'block');  
        }
      }


such as SSI_GetValue just to test if the rest of the code is working, my notes page doesn't remain displayed after I go back to it.

I am wondering if the issue is with the way that SSI Sawtooth treats inputs, or rather if it is with another part of my code or with my preventDefault command.

If anybody had any advice about this, that would be very helpful.

Thanks!
asked Jul 26 by Jake Hammond (215 points)
retagged Jul 26 by Walter Williams

1 Answer

+1 vote
A couple of things come to mind that you may want to look into:

* Your function "displaynote" doesn't appear to be getting called anywhere in the code.  You may want to move that part of the code from "displaynote" into a $(document).ready event so that it is ran when the page is ready.

* If you go the non-Sawtooth Script way, your variable "$input" is only defined if the respondent has put their focus on the input - which they can't do because it is invisible.  I would get rid of this variable and just update the if statement to look something like this instead:

if ($('#[% QuestionName() %]').val().length != 0) {


* If you go the Sawtooth Script way, you should get the length of the string SSI_GetValue returns and check whether it is non-empty, like this:

if (SSI_GetValue("e7011mnote").length != 0) {


Try making some of those changes and tell me if it works.
answered Jul 26 by Zachary Platinum Sawtooth Software, Inc. (63,675 points)
Thanks so much!

So I've tried inserting displaynote into a $(document).ready event, I tried that before too actually, but then the notes do not work at all when called by F10 or pressing the note button like they were before. Should I put the $(document).ready event at a different place in my code or could it be a different issue?

<style>
#[% QuestionName() %]_div {display:none}
</style> 

<script>
$(document).keydown(checkNote);
function checkNote(f){
    f = f || window.event;
    if (f.keyCode == '121' || f.keyCode == '35'){
        f.preventDefault(f);
        $("#[% QuestionName() %]_div").css('display', 'block');
        }
}


$( document ).ready(function(d){
    d = d || window.event;
    if ($('#[% QuestionName() %]').val().length != 0) {
        d.preventDefault(d);
        $("#[% QuestionName() %]_div").css('display', 'block');  
        )};
      }

      
function checkNoteButton(){
    $("#[% QuestionName() %]_div").css('display', 'block')
    };

</script>
$(document).ready doesn't take any arguments, so you can remove the "d" on line 16 and delete lines 17 and 19.

You have mismatched parentheses for the ready event.  Line 22 needs "})".

I think you can safely remove the "checkNoteButton" function.  It is never called.
Yes, that worked perfectly, thank you so much for your help!
...