Java Script Question | XM Community
Solved

Java Script Question

  • 2 October 2018
  • 4 replies
  • 2 views

Hello! For a project I'm working on, we need to run Java script on over 300 multiple choice questions (each of which is in a separate block). Is there a way to apply our script to multiple choice questions in bulk without copy and pasting our code into each individual question?

Current script:
Qualtrics.SurveyEngine.addOnReady(function(){
jQuery('body').keydown( function( event ) {

    switch (event.which) {
        case 49: // the 1 key
            event.preventDefault();
            jQuery(jQuery('span.LabelWrapper label')[0]).click()
            jQuery('#NextButton').click();
            break;
        case 48: // the 0 key
            event.preventDefault();
            jQuery(jQuery('span.LabelWrapper label')[1]).click()
            jQuery('#NextButton').click();
            break;
    
        default:
            // don't do anything
            break;
    }

});


For more background on the purpose of our code etc, feel free to refer to this thread: https://www.qualtrics.com/community/discussion/1480/key-press-response-issue#latest

Thank you!
icon

Best answer by TomG 3 October 2018, 15:51

View original

4 replies

Hello @anm54267 ,

You can place your code in "look and feel"-> "Advanced"("General")->"header(edit)"->"source(<>)", by doing so the script will run at each page
Userlevel 7
Badge +7
> @Shashi said:
> Hello @anm54267 ,
>
> You can place your code in "look and feel"-> "Advanced"("General")->"header(edit)"->"source(<>)", by doing so the script will run at each page

In addition to this you will need to check that the question on page is multiple choice.
@AnthonyR Can you elaborate on this?

> @AnthonyR said:
> > @Shashi said:
> > Hello @anm54267 ,
> >
> > You can place your code in "look and feel"-> "Advanced"("General")->"header(edit)"->"source(<>)", by doing so the script will run at each page
>
> In addition to this you will need to check that the question on page is multiple choice.
Userlevel 7
Badge +27
@anm54267 - @AnthonyR means that since your script will run on every page, it should only apply on pages that have one and only one multiple choice question. It should also probably only apply if the MC question has two choices. So you should put your code inside if statements:
```
if(jQuery('.QuestionOuter.MC').length == 1) //one question is MC
if(jQuery('.QuestionOuter.MC li.Selection').length == 2) { //two MC choices (vertical)
...your code goes here...
}
}
```

Leave a Reply