Welcome to the LimeSurvey Community Forum

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

Modify Image Attribute based on List Selection

More
11 years 8 months ago #102481 by dweisser
Here you are.
The topic has been locked.
More
11 years 8 months ago #102484 by tpartner
I don't get it. Here is a working test using your template - partnersurveys.com/limesurvey20/index.ph...82/newtest/Y/lang/en

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
11 years 8 months ago - 11 years 8 months ago #102485 by dweisser
Wow. Must be something local? Who knows? As I said before, i have a javascript problem with this template - but apparently that is only local as well.

I'll continue to struggle with it. I can't thank you enough.
David

Update:
T - for some reason, when I moved the cookie js to the top of template.js, it worked.
Hmm...does that mean that perhaps I have some invalid code in there?

Update2:
The only drawback to this method is that it takes a moment for the browser to read the cookie - so you see the "original" image during this time. I'm sure there is a "hack" to prevent this or mitigate this. In the meantime, i set my "original" image to a blank spacer.

You absolutely rock. Do you accept tips? Or bribes. (I'm actually serious ;) )
David
Last edit: 11 years 8 months ago by dweisser.
The topic has been locked.
More
11 years 8 months ago #102810 by dweisser
Hey T -

Why doesn't this work for a radio list? I switched "change" for the list drop down with "click" for the radio button. And I changed it back...and tried some other things as well.

$(document).ready(function(){

// Initial setting
if($('#answer171641X186X1342').val() == '1') {
$('#variableimage').attr('src', ' www.springboltconsulting.com/surveyasset...ages/demo_image1.png ');
}

// Listener on the select
$('#answer171641X186X1342').click(function(event){
if($(this).val() == '1') {
$('#variableimage').attr('src', ' www.springboltconsulting.com/surveyasset...ages/demo_image2.png ');
}
});
$('#answer171641X186X1342').click(function(event){
if($(this).val() == '2') {
$('#variableimage').attr('src', ' www.springboltconsulting.com/surveyasset...ages/demo_image3.png ');
}
});
$('#answer171641X186X1342').click(function(event){
if($(this).val() == '3') {
$('#variableimage').attr('src', ' www.springboltconsulting.com/surveyasset...ages/demo_image4.png ');
}
});

});

How did you learn this stuff?
The topic has been locked.
More
11 years 8 months ago - 11 years 8 months ago #102832 by tpartner
I'm not sure I get your image settings - in the "Initial setting" if radio 1 is checked, you want it to be demo_image1, but in the listener if radio 1 is checked, you want it to be demo_image2.

Having said that, something like this should work for radios:
Code:
$(document).ready(function(){
 
  // Initial setting
  if($('.question1342 input.radio:checked').attr('value') == '1') {
    $('#variableimage').attr('src', 'https://www.springboltconsulting.com/surveyassets/images/demo_image1.png');
  }
 
  // Listener on the radios
  $('.question1342 input.radio').click(function(event){
    var clickedvalue = $(this).attr('value');
    if(clickedvalue == '1') {
      $('#variableimage').attr('src', 'https://www.springboltconsulting.com/surveyassets/images/demo_image2.png');
    }
    if(clickedvalue == '2') {
      $('#variableimage').attr('src', 'https://www.springboltconsulting.com/surveyassets/images/demo_image3.png');
    }
    if(clickedvalue == '3') {
      $('#variableimage').attr('src', 'https://www.springboltconsulting.com/surveyassets/images/demo_image4.png');
    }
  });
});

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 11 years 8 months ago by tpartner.
The topic has been locked.
More
11 years 8 months ago - 11 years 8 months ago #102839 by dweisser
Hi TPartner -

I'm using the image tick workaround. I don't think it should interfere with the code you so graciously supplied. But, alas, I copied and pasted it directly, and it doesn't seem to work.

I activated a Test Survey here if it is possible for you to see?

Also, the image is attached to a question - not in the template...what do you think?

Thank you so much.
David
Last edit: 11 years 8 months ago by dweisser.
The topic has been locked.
More
11 years 8 months ago #102845 by tpartner
That link just leads to your survey listing page.

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
More
11 years 8 months ago #102852 by tpartner
Oops, my mistake - change both instances of ".question1342" to "#question1342"

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
11 years 8 months ago #102853 by dweisser
Completely works. You absolutely rock!
Happy holidays from Nebraska TPartner.
David
The topic has been locked.
More
11 years 6 months ago #104882 by dweisser
Hey T,
Any chance you have insight into how to modify the image attribute using a multi-select question?

If my question is 1628 and my subquestions are sq001, sq002 and sq003, I'm not sure how to adjust to accommodate this structure...as well as the fact that instead of radio buttons and dropdowns, they are check boxes.

If you could point me int eh right direction, I'd sure appreciate it.
Thanks as always,
David
The topic has been locked.
More
11 years 6 months ago #104903 by tpartner
I'm not sure how that would work if multiple checked boxes are possible.

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
11 years 6 months ago #104914 by dweisser
I would like to show an image for each check box....
So, up to three images could show if there are three subquestions. I think the logic is the same, but instead of #question1488, should it be something like #question1488SQ001. Instead of input.radio:checked').attr('value') == 'Y', should it input.checkbox:checked').attr('value') == 'True'?

Code:
$(document).ready(function(){
 
  // Initial setting
  if($('#question1488 input.radio:checked').attr('value') == 'Y') {
    $('#variableimage1615').attr('src', 'https://www.domain.com/surveyassets/images/y1488_1.png');
  }
  if($('#question1488 input.radio:checked').attr('value') == 'N') {
    $('#variableimage1615').attr('src', 'https://www.domain.com/surveyassets/images/blank1x1.png');
  }  
 
  // Listener on the radios
  $('#question1488 input.radio').click(function(event){
    var clickedvalue = $(this).attr('value');
    if(clickedvalue == 'Y') {
      $('#variableimage1488').attr('src', 'https://www.domain.com/surveyassets/images/y1488_1.png');
    }
    if(clickedvalue == 'N') {
      $('#variableimage1488').attr('src', 'https://www.domain.com/surveyassets/images//blank1x1.png');
    }    
  });
});
The topic has been locked.
More
11 years 6 months ago #104981 by tpartner
You will need to look for the "checked" property on the check-boxes.

There will certainly be more elegant ways to do it but this script should be fairly easy to follow.

Assumptions...
- Survey ID - 111111
- Group ID - 222
- Question ID - 333
- Subqestion codes - SQ001, SQ002, SQ003
- Image IDs - variableimage1, variableimage2, variableimage3

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function(){
 
    // Define the image sources
    var img1Original = $('#variableimage1').attr('src');
    var img2Original = $('#variableimage2').attr('src');
    var img3Original = $('#variableimage3').attr('src');    
    var img1New = 'https://www.domain.com/surveyassets/images/image_1_new.png';
    var img2New = 'https://www.domain.com/surveyassets/images/image_2_new.png';
    var img3New = 'https://www.domain.com/surveyassets/images/image_3_new.png';
 
    // Initial settings
    if($('#answer111111X222X333SQ001').is(':checked')) {
      $('#variableimage1').attr('src', img1New);
    }
    if($('#answer111111X222X333SQ002').is(':checked')) {
      $('#variableimage2').attr('src', img2New);
    }
    if($('#answer111111X222X333SQ003').is(':checked')) {
      $('#variableimage3').attr('src', img3New);
    }
 
    // Listeners on the 3 checkboxes
    $('#answer111111X222X333SQ001').change(function(event) {
      if($(this).is(':checked')) {
        $('#variableimage1').attr('src', img1New);
      }
      else {
        $('#variableimage1').attr('src', img1Original);
      }
    });
    $('#answer111111X222X333SQ002').change(function(event) {
      if($(this).is(':checked')) {
        $('#variableimage2').attr('src', img2New);
      }
      else {
        $('#variableimage2').attr('src', img2Original);
      }
    });
    $('#answer111111X222X333SQ003').change(function(event) {
      if($(this).is(':checked')) {
        $('#variableimage3').attr('src', img3New);
      }
      else {
        $('#variableimage3').attr('src', img3Original);
      }
    });
  });
</script>

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
11 years 6 months ago #105055 by dweisser
Once again, my sincerest thanks. Works beautifully.
David
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose