Turn displayed choices into selected choices with js | XM Community
Skip to main content

I would like to use javascript to select choices in a multi-select MC question based on unselected choices in a previous question.

I’m a little confused by how to do this because most of the related threads I’ve found are asking about selecting choices in conjunction with setting embedded data and/or other functions (and because I’m new to js). I only want to change the answer choice status -- all the other js/piped text/embedded data stuff is happening elsewhere. 

(The reason I want to force them to selected state is because I later have to do complex display logic on 200+ answer choices, and the difference between “is displayed” and “is selected” will save me 500-1000 manual menu selections, and it seems like I can get a SelectedChoicesCount to use for embedded data, but not DisplayedChoicesCount.)

Here’s the flow:

Q1: List of tv shows, including distractors; participants select the ones they’ve seen

Q2 (Hidden): List of shows that were unselected in Q1, with distractors removed via advanced randomization

Q3 (Hidden): Long list of video clips, multiple clips per stimulus show that was listed in Q1. Display logic will be used to only display a clip/answer choice if the show was displayed in Q2 and its quota hasn’t been met yet. Then blah blah complicated additional js written by a friend to do other stuff needed for the experiment].

I’d like to add js to Q2 to turn Displayed Choices into Selected Choices. (And then in Q3 I could use display logic to choose selected choices rather than displayed choices, which would save a lot of trackpadding.)

I know that I would use some variation of this:

('inputttype="checkbox"]').x.prop('checked',true);

where x tells it which boxes to check, but I’m not sure what to put in place of x that would mean ‘all displayed choices’.

    

Later in my survey, I’m doing a 7-item random selection thing from multiple questions and then selecting the answers so they can be used in loop & merge and it (thanks to js friend) looks like:

getRandomSubset( $ANF.find('input:type=checkbox]'), 7 ).prop( 'checked', true );

 

In a different help thread where the person wanted to check the 1st answer choice, it looks like:

jQuery('input type="checkbox"]').eq(0).prop('checked',true)

...and so on. 

 

Any suggestions on how to make all displayed choices the target for the checked true command?

Thank you!

You can check all the displayed choices in a MC like this:

jQuery("#"+this.questionId+" [choiceid]").prop("checked",true);

 


Thank you s much @TomG!


@TomG I overestimated my brain function. I used the code you provided, and I couldn’t figure out why it was only selecting the last answer choice in the list. I’m leaving note here in case anyone who googles to find an answer for this makes the same mistake I was making.

I was using it on a carry forward question, and that was the issue that made it only select the last answer chocie. When I made a copy of the original Q1 and put display logic on every answer choice to only display if the Q1 version is unselected and then added the js, the code worked and checked all choices.

So I guess I can’t use carry forward in this instance. 

(Back to display logic on a few hundred items, alas.)


@jinx,

The code works with carry forward. In the carry forward example above, I think you forgot to set the carry forward question to multi-select. Only the last choice being selected is indicative of it being a single select. 


@TomG You were so right! I was coming down with something (why it’s taken me so long to follow-up) and didn’t notice that the second question was still on the default ‘one answer.’ Once I updated it to multiple answers, it worked perfectly. Thank you for your patience!


Leave a Reply