Setting default choices using Javascript | XM Community
Solved

Setting default choices using Javascript

  • 21 July 2020
  • 3 replies
  • 229 views

Hello, I want to pre-select (i.e., selected by default) a checkbox that's part of a side-by-side question based on an embedded data field. So, given a value in the embedded data field a checkbox option will appear checked by default. I've been able to hide an element of the checkbox using the code below, but I don't know how to modify it to set a checkbox as checked by default. I've seen what look like complex solutions on the internet, but I have very limited knowledge of Javascript. I wonder if the code below can be adapted?
Qualtrics.SurveyEngine.addOnReady(function()
{
if("${e://Field/Skill6Included}" > 0) {
$('QR~QID9#2~6~1').up('td').childElements().invoke('hide');
}
Above, "${e://Field/Skill6Included" is the embedded data field, and "QR~QID9#2~6~1" is the option box I want to select using the embedded data field (second column below, sixth row).
Untitled.png

icon

Best answer by SurajK 21 July 2020, 22:00

View original

3 replies

Userlevel 5
Badge +4

Hi jrolison ,
Looking at your screenshot, you want to set a checkbox based on embedded data variable, you can use the below code for all 7 skill set, you can adjust your code as per your requirement, just change the eq() value,
Qualtrics.SurveyEngine.addOnReady(function()
{
//Skill 1
if("${e://Field/Skill1Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(0).prop('checked',true)
}


//Skill 2
if("${e://Field/Skill2Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(1).prop('checked',true)
}


//Skill 3
if("${e://Field/Skill3Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(2).prop('checked',true)
}


//Skill 4
if("${e://Field/Skill4Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(3).prop('checked',true)
}


//Skill 5
if("${e://Field/Skill5Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(4).prop('checked',true)
}


//Skill 6
if("${e://Field/Skill6Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(5).prop('checked',true)
}


//Skill 7
if("${e://Field/Skill7Included}" > 0) {
jQuery('#'+this.questionId).find('tbody tr').find('.SBS2').find('input[type="checkbox"]').eq(6).prop('checked',true)
}


}

Thanks! That works great!

Userlevel 2
Badge +2

Dear SurajK,
I'm trying to implement the code you provided to check boxes in a side-by-side question with just one column, but I can't get it to work. Could it be that some of the names/types (like .SBS2 ) changed?
Kind regards,
Kerwin

Leave a Reply