How to use Javascript to hide a multiple choice option if it matches an embedded data entry | XM Community
Solved

How to use Javascript to hide a multiple choice option if it matches an embedded data entry

  • 8 January 2021
  • 2 replies
  • 181 views

Hello,
I have a survey where I want members of a team to evaluate their peers. When they are in the survey, there is a multiple choice question with 100 names, where they can choose the name of the colleague they will be evaluating.
I would like to write a javascript code which hides certain names, if they match entries in the embedded data of the survey. For example, I would like to hide the name of the person who is filling the survey, and also names that such person has already evaluated (these names are stored in the embedded data). I know this can be achieved with display logic for each individual option, but I would have to insert 10 conditions for each of the 100 options.
I tried doing this, based on other topics of this forum without success, I believe this may be a simple syntax error, but I'm not experienced in Javascript so I cannot understand the issue... Can anybody help me understand the issue, please?
My goal with this code would be to get the id of the option corresponding to the name, and then hiding that id, but it does not work:
var a = getChoicesFromVariableName("${e://Field/Name}");
var id=a[0];
jQuery("#"+this.questionId+" input[choiceid=" + id + "]").closest("li").hide();
I appreciate your attention and insights.
Best Regards,
Francisco.

icon

Best answer by FranciscoL 14 January 2021, 15:53

View original

2 replies

Userlevel 6
Badge +5

I don't think you need javascript for this. You can click on an answer option and set display logic to display if Embedded data variable X Does not equal Y.

Yes, I could do it as you mentioned, as I wrote in the original post. The issue was that I had 100 options, and they can change monthly, so it would make sense to automate it through Javascript.
I found a suitable way, suggested by TomG , and I'll leave it here if anybody has the same issue:

jQuery("#"+this.questionId+" label.SingleAnswer").each(function() {
  var label = jQuery(this);
if(label.text() == "${e://Field/Name}") label.closest("li").hide();
});

Leave a Reply