Output Text based on Selection in Dropdown Menu

PennController for IBEX Forums Support Output Text based on Selection in Dropdown Menu

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5775
    AndreasK
    Participant

    Hi Jeremy,

    I would like to print text, depending on the selection that participants made in a Dropdown menu in the previous trial. I’ve written this code:

    Sequence( "intro", "check")
    
    newTrial("intro", 
        newDropDown("hand", "...")
            .add("left", "right")
            .print()
            .log()
            .wait()
            ,
        newVar("handedness")
            .global()
            .set(getDropDown("hand"))
       ,
        newButton("Next")
            .print()
            .wait()
            )
    
        newTrial("check",
            newText(getVar("handedness").test.is("left")?"Option1":"Option2")
                .print()
            ,
            newText(getVar("handedness").test.is("left")?"Option3":"Option4")
                .print()
            ,
            newButton("Next")
                .print()
                .wait()
            )

    What I would like is the following:
    – When participants select ‘left’, they see ‘Option1 Option3’ after they click on the button.
    – When participants select ‘right’, they see ‘Option2 Option4’ after they click on the button.

    However, the code always prints ‘Option1 Option3’, regardless of which option from the DropDown menu is selected.

    Could you help with that?

    Andreas

    #5777
    Jeremy
    Keymaster

    Hi Andreas,

    You’re mixing up plain javascript code and PennController code. The PennController test commands are meant to be used autonomously (ie. as a block of commands separated by commas, or inside a callback command for example, just like a newText command for example) not inside a javascript triconditional like this. You need to use success and/or failure so you can execute PennController commands conditionally. So this is what you second trial should look like:

    newTrial("check",
        newText("optionA").print(),
        newText("optionB").print()
        ,
        getVar("handedness").test.is("left")
            .success( getText("optionA").text("Option1"),getText("optionB").text("Option3") )
            .failure( getText("optionA").text("Option2"),getText("optionB").text("Option4") )
        ,
        newButton("Next")
            .print()
            .wait()
    )

    Jeremy

    #5780
    AndreasK
    Participant

    Hi Jeremy,

    Thank you for your explanation and for the code. It works perfectly.

    Andreas

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.