Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Expressions in dropdown fields + hide empty dropdown fields

  • snieuw
  • snieuw's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 3 months ago #89810 by snieuw
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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago #89829 by tpartner
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.
Code:
<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.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose