Can I use JavaScript instead of Display Logic? | XM Community
Skip to main content
Question

Can I use JavaScript instead of Display Logic?


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Level 5 ●●●●●
  • 137 replies

I have a matrix table with 300 columns using the reusable choice list. 

 

I’d like to display columns based on an answer to a text entry question earlier in the survey, where the respondent answers with a value between 0 and 300.

 

If the respondent answers say, 10, then I’d only want to show the last 10 columns in the matrix table i.e. hide the first 290 columns.

If the respondent answers say, 200, then I’d want to show the last 200 columns in the matrix table i.e. hide the first 100 columns.

 

I appreciate that this is possible with Display Logic, but I assume would be very painful.

 

Thanks

4 replies

Shashi
Level 8 ●●●●●●●●
Forum|alt.badge.img+32
  • Level 8 ●●●●●●●●
  • 633 replies
  • October 8, 2023

Use the below code in the OnReady of the matrix question. Since we have large scale point I assume you are using drop down list matrix question.

	var n = parseInt(10)||300;
	jQuery("#"+this.questionId+" .ChoiceRow").each(function(){
		var s = jQuery(this).find("select");
		jQuery(s).find("option").hide();
		jQuery(s).find("option:first").show();
		var c =  jQuery(s).find("option:last").index();
		for(var i =1;i<=n;i++){
			jQuery(s).find("option:eq("+c+")").show();
			c--;
		}		
	});

Change the value of ‘n’ from 10 to pipe in of the text entry question.


rgupta15
Level 4 ●●●●
Forum|alt.badge.img+8
  • Level 4 ●●●●
  • 92 replies
  • October 8, 2023

Yes, it is possible to code this using javaScript in JS section. I’d suggest to use .css(‘display’,’inline) & .css(‘display’,’none’) to show and hide your choices respectively.

var n; #update the value of n as intended.

var hide=300-n

for(i=0;i<hide;i++)

{

jQuery('.ChoiceRow').eq(i).css('display','none')

}


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • 137 replies
  • October 8, 2023
Shashi wrote:

Use the below code in the OnReady of the matrix question. Since we have large scale point I assume you are using drop down list matrix question.

	var n = parseInt(10)||300;
	jQuery("#"+this.questionId+" .ChoiceRow").each(function(){
		var s = jQuery(this).find("select");
		jQuery(s).find("option").hide();
		jQuery(s).find("option:first").show();
		var c =  jQuery(s).find("option:last").index();
		for(var i =1;i<=n;i++){
			jQuery(s).find("option:eq("+c+")").show();
			c--;
		}		
	});

Change the value of ‘n’ from 10 to pipe in of the text entry question.

Hi @Shashi 

Actually it is really 300 columns in a matrix multi choice question. Can I adjust the code above to hide columns rather than rows?

Thanks 


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • 137 replies
  • October 8, 2023
rgupta15 wrote:

Yes, it is possible to code this using javaScript in JS section. I’d suggest to use .css(‘display’,’inline) & .css(‘display’,’none’) to show and hide your choices respectively.

var n; #update the value of n as intended.

var hide=300-n

for(i=0;i<hide;i++)

{

jQuery('.ChoiceRow').eq(i).css('display','none')

}

Hi @rgupta15 

Can I use the code above to hide columns?

Thanks

 


Leave a Reply