- Posts: 215
- Karma: 5
- Thank you received: 6
- Forum
- English support forums
- Can I do this with LimeSurvey?
- Modify Image Attribute based on List Selection
Modify Image Attribute based on List Selection
I'm trying to show an image based on an answer selections in a list-type question.
The item is 856216X180X1269 and the value codes for answers are 1,2,3 etc.
In my template question.pstpl, I have <img id="variableimage" src="path/currentimage />. This is the img in which I would like to alter the src based on the response.
In template.js, I have this:
$(document).ready(function() {
if ($('#java856216X180X1269').attr.value =='1' ) {
$('#variablelogo').attr('src', ' www.fullpath.com/surveyassets/images/image1.png ');
}
})
I hoped that if they select Answer Option 1, then Image 1 would appear. I think I would have to tie this to the change event of the question, but I'm afraid this is beyond me. This is what I have so far - and it doesn't work. It would be grand if someone could point me in the right direction.
Thank you in advance,
David
Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
Is it a radio or dropdown list?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
$(document).ready(function(){
// Initial settting
if($('#answer856216X180X1269').val() == '1') {
$('#variablelogo').attr('src', 'www.fullpath.com/surveyassets/images/image1.png');
}
// Listener on the select
$('#answer856216X180X1269').change(function(event){
if($(this).val() == '1') {
$('#variablelogo').attr('src', 'www.fullpath.com/surveyassets/images/image1.png');
}
});
});
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
So, a couple of things.
First, the onchnange event works fine, except if you push next and then previous. The answers are still set...but the image has reverted to its initial setting. Any thoughts?
Second, the survey is group by group. The <img id="variablelogo /> only shows on the group page where the trigger question is located. I think I could include a hidden question in each group equal to the value of the trigger dropdown. Then, I could use the javascript to reference those questions, one for each group...
But, is there a more elegant solution? Again, as always, any direction is appreciated.
David
Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
2) Do you need to show the modified image in subsequent groups?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.
So, when you first go to the page: '#answer856216X180X1269').val() == '', right? So my initial image shows as planned. When I "change" the drop down, to val()=='1', i see image one successfully.
If I push next, but then push previous, the drop down shows the answer selection associated with val()=='1', (that is, I see the value I selected). But, the associated image is gone as is the initial image. No image is shown.
And yes, I'd like to show the image in subsequent group.
Don't know if its relevant, but the initial state of the drop-down includes an answer option "Please choose...", but once you make a selection and push next then push previous, that answer option is no longer available.
Please Log in or Create an account to join the conversation.
First, the onchnange event works fine, except if you push next and then previous. The answers are still set...but the image has reverted to its initial setting. Any thoughts?
Because when you push previous, you don;t activate the change event for the drop down. I think this can be solved by populating a hidden question with the value of the drop down and adjusting the javascript to key off of that. It means one hidden question per group - which isn't too bad.
What do you think?
Please Log in or Create an account to join the conversation.
Let me clear cache again...

Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
Try this in template.js. It will detect the drop-down answer and set a cookie that is accessed in all pages to set the image path.
$(document).ready(function(){
// If the dropdown is on this page...
if($('#answer856216X180X1269').length > 0) {
// Initial cookie setting
var imageSetting = $('#answer856216X180X1269').val();
eraseCookie('ls_image_setting');
createCookie('ls_image_setting', imageSetting, '');
// Listener on the select
var originalPath = $('#variablelogo').attr('src');
$('#answer856216X180X1269').change(function(event){
if($(this).val() == '1') {
$('#variablelogo').attr('src', 'http://www.fullpath.com/surveyassets/images/image1.png');
}
else {
$('#variablelogo').attr('src', originalPath);
}
var imageSetting = $(this).val();
eraseCookie('ls_image_setting');
createCookie('ls_image_setting', imageSetting, '');
});
}
//Check the cookie and set the image accordingly
var imageSetting = readCookie('ls_image_setting');
if(imageSetting == '1') {
$('#variablelogo').attr('src', 'http://www.fullpath.com/surveyassets/images/image1.png');
}
});
// NO EDITING REQUIRED BELOW HERE
// Some cookie managemnent tools
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.
I thank you tremendously for the code. I'm afraid though that I cannot get the image to show in Group 2; it still reverts.
Would you believe that I've literally been working on this all day? It has been maddening. Perhaps the cookie has not being set?
I got the image to show in Group2 by storing a value "1", "2", etc in the window.name property.
In the onchnage event, I included the line:
window.name = "1";
Then the function:
window.onload = function() {
var myImage = document.getElementById("variablelogo");
myImage.src = "fullpath/originallogo.png";
if(window.name == '1') {
myImage.src = "fullpath/logo1.png";
}
};
However, I couldn't get the image to show up in the original place when I pressed the previous button.
I like the cookie implementation - window.name feels hacky.
Thoughts?
Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
Here is a sample survey and template. Install the template before the survey. After importing the survey, modify all instances of "752182X9X27" in template.js to match the new IDs.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.
You've done more than enough. When I download and install your template and survey - it works as intended. But, not with my template. The changeevent works, but subsequent groups do not reflect the image.
I'm using a citronade clone - it has some issues with javascript outlined in this post .
Would it possibly be due to the fact that your survey has answer codes "A1", "A2", etc. and mine has "1", "2", etc? I changed template.js to reflect this in two places...but perhaps their length is relevant? I'm dreading having to undo all of my work and apply it to a new template for this.
Thank you Tpartner. More than gracious assistance.
David
Please Log in or Create an account to join the conversation.
- Posts: 7791
- Karma: 618
- Thank you received: 2280
No, the response code length should have no affect.
Can you attach an export of your template?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in or Create an account to join the conversation.