Bienvenue, Invité
Nom d'utilisateur : Mot de passe : Se souvenir de moi

SUJET : Expressions in dropdown fields + hide empty dropdown fields

Expressions in dropdown fields + hide empty dropdown fields il y a 1 an 11 mois #89810

  • snieuw
  • Portrait de snieuw
  • Hors ligne
  • Fresh Lemon
  • Messages : 5
  • Karma: 0
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
Pièces jointes :
L'administrateur a désactivé l'accès en écriture pour le public.

Re: Expressions in dropdown fields + hide empty dropdown fields il y a 1 an 11 mois #89829

  • tpartner
  • Portrait de tpartner
  • En ligne
  • LimeSurvey Team
  • Messages : 4372
  • Remerciements reçus 830
  • Karma: 381
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
L'administrateur a désactivé l'accès en écriture pour le public.
Modérateurs: ITEd
Temps de génération de la page : 0.133 secondes
Donation Image