Welcome to the LimeSurvey Community Forum

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

Need some help with a code snippet please

  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 7 months ago #139720 by lintu
Hello,

On the template editor, I am trying to insert some html code based on certain survey question # using JavaScript. I tried few things based on few of the forum posts, but nothing is working for me. Can someone help me out with a code snippet? Thanks a lot!!

I am using: LimeSurvey Version 2.50+ Build 160620
PHP 5.3.3
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139724 by DenisChenu
Replied by DenisChenu on topic Need some help with a code snippet please

lintu wrote: ....
I tried few things based on few of the forum posts, but nothing is working for me.....

Best is to staring by explain what did you trye, and how do you see it don't work.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139742 by tpartner
Replied by tpartner on topic Need some help with a code snippet please

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 7 months ago - 7 years 7 months ago #139775 by lintu
Replied by lintu on topic Need some help with a code snippet please

Best is to staring by explain what did you trye, and how do you see it don't work.

Denis


Here's the code that I tried on the question.pstpl template:
Code:
<script type="text/javascript" charset="utf-8">
   var q = {QUESTION_CODE};
   if q == 'xyz'{
      document.write("blah blah");
   }
  else { //do nothing}
</script>

My goal is to display an image based on the survey question # or question code, but the above code is not working. Any idea why?
Thanks
Last edit: 7 years 7 months ago by lintu.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139778 by Joffm
Replied by Joffm on topic Need some help with a code snippet please
Hi, lintu,

well, in the question.pstpl you have the "look and feel" of the question container.
Do you want to display a background image of the question container depending on the question code?
Or just an image in the question?
Or where?

Regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139786 by tpartner
Replied by tpartner on topic Need some help with a code snippet please
I'm not sure why you wouldn't simply insert the image in the question source.

Having said that, there are a couple of errors in your JavaScript:
1) You need to ensure the document is fully loaded before detecting the question code
2) {QUESTION_CODE} needs to be enclosed in quotes (it's a string, not a var)

Code:
<script type="text/javascript" charset="utf-8">     
  $(document).ready(function() {  
    var q = '{QUESTION_CODE}';
    if (q == 'Q1') {
      document.write('This is '+q+'!');
    }
    else { 
      // Do nothing
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: lintu
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139792 by DenisChenu
Replied by DenisChenu on topic Need some help with a code snippet please
And document.write("blah blah"); must be used in HTML source only. Not in external js .

Then use jquery : $('body .content').append("'This is '+q+'!'); for exemple

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: lintu
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 7 months ago #139800 by lintu
Replied by lintu on topic Need some help with a code snippet please
Joffm, DenisChenu & tpartner,

Really appreciated that you took the time to respond and help. I am displaying an image on top of the answer selections (see screenshot below) for every question. Since I have over 100+ questions, instead of adding this to each answer, I inserted my code in the question.pstpl template. Now, for select questions, I want to hide this based on the question # or question code. I have turned on the option to show the question code and tried the code example that tpartner provided, but that did not work. Will try what DenisChenu suggested next.



Thanks
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago #139803 by Joffm
Replied by Joffm on topic Need some help with a code snippet please
Hi, lintu,
here a different approach to remove the picture in certain questions.

What about the way to do it in EXCEL.
www.limesurvey.org/de/foren/future-featu...ate-survey-in-script
and
manual.limesurvey.org/Excel_Survey_Structure

Export your survey to a TAB-separated text file, open it in EXCEL (utf-8) and remove the values in columns 37 (dualscale_headerA) and 38 (dualscale_headerB).
Then import it again.

If you are not successful with the JavaScript, this could help you. And it is done rather quickly.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: lintu
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 7 months ago #139804 by lintu
Replied by lintu on topic Need some help with a code snippet please
Joffm,

Wow, I wasn't aware of this cool feature, will definitely try it out and report back. Thanks a lot!!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 7 months ago - 7 years 7 months ago #139812 by tpartner
Replied by tpartner on topic Need some help with a code snippet please

I have turned on the option to show the question code and tried the code example that tpartner provided, but that did not work.

Try setting display question number and code to "Both". (displaying just the code didn't work for me either - maybe a bug)

And, yes, insert Denis' snippet instead of document.write. (I was just using that to demonstrate that the code works)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 7 months ago by tpartner.
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 7 months ago #139821 by lintu
Replied by lintu on topic Need some help with a code snippet please
Here is where I am stuck - I am now getting the value of {QUESTION_CODE}' and the JavaScript if/else statement works. But having an issue with displaying the image at the right spot. I tried using the code suggested by DenisChenu - $('body .content').append("'This is '+q+'!'); but the image is not displaying. Is the syntax correct? If I change the syntax to document.body.appendChild(img); then the image displays, but at the bottom of the page.

Any suggestions? Thanks.

P.S. Joffm - I exported the survey excel file as you suggested, but my custom code is not in there for me to remove as I inserted it in the template.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose