Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: Modify Image Attribute based on List Selection

Modify Image Attribute based on List Selection 2 years 7 months ago #102400

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
Hi all,

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
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102406

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
Hi David,

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.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102413

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
It is a drop-down list.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102420

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
Try this:

	$(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.
Last Edit: 2 years 7 months ago by tpartner.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102436

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
Works beautifully! Thanks again TPartner.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102438

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
Hey Tpartner - -
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
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102445

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
1) The "Initial setting" section in the code should take care of that. Are you getting ant JavaScript errors?

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.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102446

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
No errors.

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.
Last Edit: 2 years 7 months ago by dweisser. Reason: More information
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102447

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
I think I understand why the image "reverts".

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?
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102448

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
It turns out that the hidden question solution does not work. Now I am officially stuck.
Let me clear cache again...:-)
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102455

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
Okay, if you want the image handled on subsequent pages, it would probably be easiest to set a cookie.

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.
Last Edit: 2 years 7 months ago by tpartner.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102457

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
Hi Tpartner,
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?
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102459

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
Hi David,

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.

File Attachment:

File Name: test_image_cookie.zip
File Size:86 KB

File Attachment:

File Name: limesurvey...2182.lss
File Size:14 KB
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 2 years 7 months ago by tpartner.
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102460

  • dweisser
  • dweisser's Avatar
  • Offline
  • Gold Lime
  • Posts: 222
  • Thank you received: 5
  • Karma: 5
TPartner,
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
Last Edit: 2 years 7 months ago by dweisser. Reason: more info
The administrator has disabled public write access.

Modify Image Attribute based on List Selection 2 years 7 months ago #102480

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6111
  • Thank you received: 1356
  • Karma: 532
Hi David,

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.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.365 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form