Welcome to the LimeSurvey Community Forum

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

Insert text depended on previous answer

  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago #152735 by SurveyDennis
Insert text depended on previous answer was created by SurveyDennis
I would like to show a different text/image depended on a previous answer.


I am trying to do this with JS but not getting it correctly.
I tried it with EM but not totally understanding how I could do this.

Anyone an idea.


The example I have now is:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

if($('#answer279829X201X799SQ001').attr('checked', true)) { alert ('yes 1 checked'); };
if($('#answer279829X201X799SQ002').attr('checked', true)) { alert ('yes 2 checked'); };

});
</script>

Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #152736 by holch
Replied by holch on topic Insert text depended on previous answer
I can't help with Javascript, but with EM this should be fairly easy.
What type of question do you use? For a single choice question for example you could do comething like this:
Code:
{if(Q1==1, "<img src='imageurl/img01.jpg' />", if(Q2==2, ""<img src='imageurl/img02.jpg' />","")}

Not tested.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago #152737 by SurveyDennis
Replied by SurveyDennis on topic Insert text depended on previous answer
Thanks, I tried this with EM but can I just add that in the question text? instead of the relevance eq.?

My question label is Q1 and my answer options are SQ001 and SQ002.
If I try it this way I only see the code itself in the question.



Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
Attachments:
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago - 7 years 6 months ago #152738 by holch
Replied by holch on topic Insert text depended on previous answer
You can use EM almost anywhere (answer options, subquestions, question text, etc.).

This is why I asked you in my previous response which question type you are using, because it is different on how you access it.

Seeing that you are using SQ001 etc., I assume that you use a question type that has subquestions (multiple answers, array).

In this case you need to proceed a little different. Would you mind sharing the question itself and what you are trying to show and when? Because multiple choice are different to matrix/array questions. E.g. for multiple choice you would probably need something like Q1_SQ001=="Y" to check if the check box has been checked, while for array questions you usually have various codes that could apply, e.g. Q1_SQ001 >= 4.

If you can provide these two questions in a LSS or LSG file, I can have a look at it. It is very simple with EM, but you need to know the structure of the questions to be able to check those things.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Last edit: 7 years 6 months ago by holch.
The topic has been locked.
  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago - 7 years 6 months ago #152739 by SurveyDennis
Replied by SurveyDennis on topic Insert text depended on previous answer
Thanks for your detailed information.
Sorry my mistake with the multi response question.
It had to be a single response instead.

I have setup an example here:
www.surveyfriendly.com/ls1/index.php/sur...67/newtest/Y/lang/en

Q1 - Brand?
A1 Brand1
A2 Brand2
A3 Brand3

Depended on the brand I want to add an additional text.
For example
Brand 1 is only available in the color blue, etc.

I think it is just something on my syntax, I have now;

{if(Q1==A1, "Blue", if(Q1==A2, "Red", if(Q1==A3, "Yellow")}

Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
Last edit: 7 years 6 months ago by SurveyDennis.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago - 7 years 6 months ago #152740 by holch
Replied by holch on topic Insert text depended on previous answer
OK, I rebuilt your scenario to be able to test it and there are various problems in your code (that is why I asked for an export, I could have just imported your example and worked on it, without building my own one):
- There were missing a few ")" to close the IFs
- If you have answer codes that have a letter I think you need to put them into parentesis.
- It seems to be necessary to have something in the "else" part, and if it is just an empty string ("").

When you save and close the question, you can usually see your equation and if there is something wrong, LS gives you pretty good indications what is wrong (just hover over the errors with your mouse). For example without the "" the A1 was considered an undefined variable, so you know that EM doesn't recognize it. That helped me to notice that it needs to be "A1", not A1.
Please find a working sample attached to this post.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Last edit: 7 years 6 months ago by holch.
The following user(s) said Thank You: SurveyDennis
The topic has been locked.
  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago #152745 by SurveyDennis
Replied by SurveyDennis on topic Insert text depended on previous answer
Sorry about that - I indeed could attach the survey.
Many thanks for your effort to rebuild it.
I see now where I missed the extra quotes.

Very powerfull this EM, just starting to play around with this!

Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #152746 by holch
Replied by holch on topic Insert text depended on previous answer
No problem. I know that you give back to the community, so it is a pleasure to help.

Yes, EM is very powerful and really helped LS to progress. You can do a lot of things with EM, besides relevance equations (which are so much quicker and more flexible than conditions).

Have a look at the manual, there are very interesting examples that can give you inspiration. But I feel like today there are already so more examples of great use of EM that the examples in the manual look 'basic'.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago #152753 by SurveyDennis
Replied by SurveyDennis on topic Insert text depended on previous answer
Thank you for the kind words.
I am documenting all my examples and will share them soon as well to all Limesurvey users.

I will look at the EM manual and examples.

Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
The topic has been locked.
  • SurveyDennis
  • SurveyDennis's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 6 months ago #153012 by SurveyDennis
Replied by SurveyDennis on topic Insert text depended on previous answer
Sorry just on more question.
Seems I can't use html tags in there?

{if(PATRAN2 == 'A1', "<button class='btn btn-info btn-smg' data-bs-target='#myModal1' data-bs-toggle='modal' type='button'>Haga clic aquí para ampliar</button>", " ")}

Or do I need to define this different?

Be SurveyFriendly too! Fight against boring surveys! www.SurveyFriendly.com
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #153018 by holch
Replied by holch on topic Insert text depended on previous answer
I am not sure, but I think the html tags need to be masked. I think tpartner posted an example a while ago. But I am not sure about this.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose