How to remove options from a question based on a condition? | Experience Community
Skip to main content
Solved

How to remove options from a question based on a condition?

  • Jan 4, 2021 4:46 PM UTC
  • 3 replies
  • 350 views

I have a question where users can select a time of day to set up an appointment. I am comparing the time in each option to the current system time, and want to remove it if it has already passed. For example, if it is 11:30am, then 9:00am, 10:00am, and 11:00am should be removed.
My code seems to be correctly identifying which options should be removed, but I'm having trouble actually removing the options.
// get system local time
var d = new Date();
var m = d.getMinutes();
var h = d.getHours();
if(h == '0') {h = 24}

var curr_time = h*60+m;
var qid = this.questionId;
var choice_contents = Qualtrics.SurveyEngine.QuestionInfo[qid].Choices;
var options = [];

Object.entries(choice_contents).forEach(item => {options.push(item[1]);})

options.forEach((item) =>{
    var hr = item.Text.split(":")[0]*60;
    if(item.Text.split(":")[1].includes("pm") && hr<720){hr +=720;}
    var mn = hr + Number(item.Text.split(":")[1].replace(/\\D/g,""));
    if(mn    jQuery("#"+this.questionId+" -label").closest("li").hide();
    }
})
I have seen the

jQuery("#"+this.questionId+ ... .hide();
snippet of code that can remove options, but I don't know what conditions to use in this case.
Any suggestions? Thank you!

Best answer by TomG

Two things:

  1. You need to use qid instead of this.questionId in your selector. 'this' no longer refers to the question object inside the forEach function.

  2. You've made it more complex by creating an object that separates your logic from the actual HTML element. However, I think this might work:

jQuery("#"+qid+" [choiceid="+item.VariableName+"]").closest("li").hide();

View original

3 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • Jan 4, 2021 5:20 PM UTC

Why are you asking the same question again? If you're choosing to pick up the solution from the previous question and it's not working, why aren't you asking over there itself?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • Jan 4, 2021 5:32 PM UTC

Two things:

  1. You need to use qid instead of this.questionId in your selector. 'this' no longer refers to the question object inside the forEach function.

  2. You've made it more complex by creating an object that separates your logic from the actual HTML element. However, I think this might work:

jQuery("#"+qid+" [choiceid="+item.VariableName+"]").closest("li").hide();


  • Author
  • Jan 5, 2021 4:51 PM UTC

TomG Awesome, that worked! I was changing the Variable Names for the options, which was causing your code to not work. Once I changed them back to 1,2,3,4, etc., it worked just fine.