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

More
9 years 1 month 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.
More
9 years 1 month ago #139724 by DenisChenu

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 support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
More
9 years 1 month ago #139742 by tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
9 years 1 month ago - 9 years 1 month ago #139775 by lintu

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: 9 years 1 month ago by lintu.
The topic has been locked.
More
9 years 1 month ago #139778 by Joffm
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.
More
9 years 1 month ago #139786 by tpartner
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.
More
9 years 1 month ago #139792 by DenisChenu
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 support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: lintu
The topic has been locked.
More
9 years 1 month ago #139800 by lintu
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.
More
9 years 1 month ago #139803 by Joffm
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
www.limesurvey.org/manual/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.
More
9 years 1 month ago #139804 by lintu
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.
More
9 years 1 month ago - 9 years 1 month ago #139812 by tpartner

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: 9 years 1 month ago by tpartner.
The topic has been locked.
More
9 years 1 month ago #139821 by lintu
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.
More
9 years 1 month ago #139822 by tpartner
Denis' $('body .content') selector was just an example. The correct selector would depend on your template. Can you activate a test survey and give us a link and also attach a mockup of where you want the image?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
9 years 1 month ago - 9 years 1 month ago #139824 by lintu
tpartner,

Here's a link to my test assessment: Link

There are two questions on this assessment - on the first question, I am displaying two images on top of the answers by inserting code in the question.pstpl template. I want to hide these images for the 2nd question.

Thanks in advance for all your help!!
Last edit: 9 years 1 month ago by lintu.
The topic has been locked.
More
9 years 1 month ago #139833 by Joffm

lintu wrote: 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.


Hi, lintu,
of course not.
This EXCEL file is the survey structure, more or less the same as the *.lss.

What I suggested was, that you remove the picture code in columns 37 (dualscale_headerA) and 38 (dualscale_headerB) in the desired questions.
By filtering the file by question code this is done very quickly - without the JavaScript stuff.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose