Expressions in dropdown fields + hide empty dropdown fields

More
4 years 8 months ago #89810 by snieuw
snieuw created the topic: Expressions in dropdown fields + hide empty dropdown fields
Hi everybody,
I would like to have an if(....) statement in one of the optional answer of a dropdown question. this should either show a certain string, or leave this field empty. afterwards, i wanted to hide empty anwers.
I have done this with array questions (works perfectly!), but somehow this expression is not executed here.

Maybe because it is in the same questiongroup? Anything I can do, to make this work in one questiongroup? And even if this DOES work: how do i hide empty elements?

Example survey is attached. it should show "a","b","c","d" if "yes" is selected, otherwise "a","b","d"

Sorry for the two combined questions, but i figured they were connected...

Thanks a lot for every hint or suggestion!!

Steffen
Attachments:

Please Log in to join the conversation.

More
4 years 8 months ago #89829 by tpartner
tpartner replied the topic: Expressions in dropdown fields + hide empty dropdown fields
I can't speak to why EM is not piping the text, you may want to file a bug report .

Regarding the hiding/showing of the drop-down option, this gets a little tricky. You can't hide the option across all browsers with "display;none" so you need to create a clone of it and then completely remove or add it to the select (drop-down) element as required.

Below is some code to do this with your example survey.
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
 
		var dynamicOption = 'A2'; // Answer code of the dynamic dropdown option
		var toggleRadio = 'A1'; // Answer code of the "Yes" radio answer
 
		// Create a clone of the dynamic option
		var optClone = $('option[value="'+dynamicOption+'"]').clone();
 
		// Define the element directly preceding the dynamic option
		var optPre = $('option[value="'+dynamicOption+'"]').prev('option');
 
		// Initially remove the dynamic option
		$('select option[value="'+dynamicOption+'"]').remove();
 
		// Listener on the radios
		$('input.radio').click(function(){
			// If "Yes" selected, insert the dynamic option
			if($(this).attr('value') == toggleRadio) {
				if($('select option[value="'+dynamicOption+'"]').length == 0) {
					$(optPre).after(optClone);
				}
			}
			// Otherwise remove the dynamic option
			else {
				$('select option[value="'+dynamicOption+'"]').remove();
			}
		});
	});	
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Did you already participate in our customer survey?

Don't miss your chance for great prices.

Please click here to participate:

Start now

Start now!

Just create your account and start using Limesurvey today.

Register now