Have an idea?

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

Dynamically hide checkboxes based on previous question

Hi, im working on a survey in SSI Web V7 and im looking to try and dynamically hide checkboxes for answers selected in each column on a previous question.

The setup is:
sc5b - Grid question with up to 5 columns and 12 answer options.

sc6b - Grid question with the same columns from sc5b, answer options selected at sc5b should not be selectable at sc6b

I have been trying to implement something like the below but i dont think the var is capturing the checkbox value so it just hides everything by default

<script type="text/javascript">;

var $r1c1 = "[%sc5b_r1_c1%]";
var $r2c1 = "[%sc5b_r2_c1%]";

if($r1c1 = 1 )
{
document.getElementById("sc6b_r1_c1_graphical").visibility = 'hidden';
document.getElementById("sc6b_r1_c1_graphical").disabled = 'true';
document.getElementById("sc6b_r1_c1_graphical").className = 'HideElement';
}

if(r2c1 = 1 )
{
document.getElementById("sc6b_r2_c1_graphical").visibility = 'hidden';
document.getElementById("sc6b_r2_c1_graphical").disabled = 'true';
document.getElementById("sc6b_r2_c1_graphical").className = 'HideElement';
}


</script>

If anyone can assist it would be amazing.

Cheers
asked Jun 2, 2021 by JamesL (140 points)
Remember that in most programming languages, we use "=" to set the left-side variable to the right-side value.  We need to use "==" to measure whether the left-side and right-side values are equal.  Try that in your "if" statements.
And should ...

if(r2c1 = 1 )

be ...

if($r2c1 == 1 )

Missing "$".
Good catch.  To clarify, James, JavaScript allows variable names to start with a "$," but it is not a necessary step like in Perl.  You can use "r2c1" or "$r2c1" - the former is probably more common in the JS community, but either will work.
Hi both,

Thank you for the answers, it seems to be working now but only if the statments are in the order that the code is (ad the first question is currently randomised). I assume it would be preferental to create a loop for this, i have been trying to edit the below which i found in a previous answer with no luck

<script>
$(document).ready(function(){
    var arr = [1, 2, 3, 4, 5, 6, 10];
    arr.forEach(function(item){
        SSI_DisableGraphicalInput('[% QuestionName() %]_r7_c' + item);
        $('#[% QuestionName() %]_r7_c' + item + '_graphical').hide();
    });
})
</script>

The question has 9 rows that need testing with up to 5 columns
Looping is always a good idea when doing repeated work like this, though I don't know why your script would care about the order of conditionals.  Is there some other script happening on the page?

The code you found from another post requires jQuery and the Better Lighthouse Library.  jQuery doesn't come baked into SSI Web v7.  Have you loaded it in your study?
Hi Zachary,
There is some perl to change question depending on previous responses but no other code on the page.

Yes jquery is loaded, i tried implementing the Better Lighthouse Library element but kept getting error messages and random text at the top of the page with that incorporated.
We'll need to resolve why the BLL isn't working if we're going to use that code.  What error messages have you seen?  What text appeared on the page?

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.
...