1

I am hoping that you can help me out. I need to change an image based on the selection of 2 dropdown values. I have a number of image variations to show any option. I have a selection of code that I know concernates the file name of the image, but I can't seem to find a way to actually display that image. I have taken the code from a previous answer, but I can't get it to work for me - Changing image based on selection in 2 dropdowns.

Here is the code that I am using:

$('select.form-control').change(function() {
    var file = $('#shape').val() + '-' + $('#waist').val() + '.jpg';
    $('#imgToChange').prop('src', filename);
});
<select class="form-control" id="shape" name="shape">
  <option value="a">Select a country</option>
  <option value="b">Canada</option>
  <option value="c">U.S.A.</option>
</select><br>
<select class="form-control" id="waist" name="waist">
  <option value="1">Select a country</option>
  <option value="2">Canada</option>
  <option value="3">U.S.A.</option>
  <option value="4">India</option>
</select><br>
<img id="imgToChange">

Where am I going wrong on this one?

Thanks in advance!

2 Answers2

1

The variable you stored the image filename is different from the variable you passed as the value in for the image's src tag

$('select.form-control').change(function() {
   var filename = $('#shape').val() + '-' + $('#waist').val() + '.jpg';
   $('#imgToChange').attr('src', filename);
});

You can also try using the .attr() method in place of the .prop() method like i did

Updated See code below HTML

<select id="select-1">
    <option value="brown-head">Brown Head</option>
    <option value="black-head">Black Head</option>
    <option value="blue-head">Blue Head</option>
</select>
<select id="select-2">
    <option value="brown-body">Brown Body</option>
    <option value="black-body">Black Body</option>
    <option value="blue-body">Blue Body</option>
</select>
<img id="imgElem" src="" alt="">

JS

var imageSrc;
var imgFolder = "/path/to/image/folder/"; // specify the path to the folder containing the imgs in this variable

$("select").change(function(){
    imageSrc = imgFolder + $("#select-1").val() + "-" + $("#select-2").val() + ".jpg";
    $("#imgElem").attr("src", imageSrc);
    $("p").text(imageSrc);
});

UPDATED ONCE AGAIN

This update creates the img tag each time an option is selected, replacing the previous img tag. This is to prevent the img src(undefined) error on page load.

To archive this, i added a div in the html to hold the img element, and simply change the divs html content.

HTML UPDATED

<select id="select-1">
    <option value="brown-head">Brown Head</option>
    <option value="black-head">Black Head</option>
    <option value="blue-head">Blue Head</option>
</select>
<select id="select-2">
    <option value="brown-body">Brown Body</option>
    <option value="black-body">Black Body</option>
    <option value="blue-body">Blue Body</option>
</select>

<p></p> <!--  just for debugging -->
<div class="img-wrap"></div> <!-- a div to hold the image -->

JS UPDATED

// specify the path to the folder containing the imgs in this variable
var imgFolder = "/path/to/image/folder/"; 

$("select").change(function(){
    var src; // variable to hold the src
    
    src = imgFolder + $("#select-1").val() + "-" + $("#select-2").val() + ".jpg";
    // append src to a p element just for debugging sake(i.e to see the change in the src)
    $("p").text(src);
    
    // create an img element with the src each time and append to the image wrap div
    $(".img-wrap").html(`<img src="${src}">`);
});

UPDATED ONCE AGAIN

To make it show the image with the default selected options both on page load and on option select, i tweaked the code again

NEW JS CODE

function getImageSrc(){
    // specify the path to the folder containing the imgs in this variable
    var imgFolder = "/path/to/image/folder/"; 

    // loop through each select element
    $("select").each(function(){
        // fire function to get src
        getSrc();
        
        // fire function to get src on change
        $(this).change(function(){
            getSrc();
        })
    });
    
    function getSrc(){
        var src; // variable to hold the src

        src = imgFolder + $("#select-1").val() + "-" + $("#select-2").val() + ".jpg";
        $("#imgElem").attr("src", src);
        // append src to a p element just for debugging sake(i.e to see the change in the src)
        $("p").text(src);

        // create a an img element with the src each time and append to the image wrap div
        $(".img-wrap").html(`<img src="${src}">`);
    }
}

// fire function on page-load
$(document).ready(function(){
    getImageSrc();
})
Redemption Okoro
  • 349
  • 1
  • 11
  • Thanks, I did miss that one. It still isn't outputting though, is it something to do with the tag? – Andrew Royal Nov 14 '20 at 03:25
  • You're getting the image element correctly, i have updated my answer, check to see if it works for you. – Redemption Okoro Nov 14 '20 at 09:27
  • Hi, this solution also works when it is in a Fiddle, but the moment I try open it in a browser, I get a. src(undefined) output. – Andrew Royal Nov 15 '20 at 02:29
  • Do you get the error on page load or when you click the select element – Redemption Okoro Nov 15 '20 at 05:59
  • It is on page load. so it shows src(unknown) on load. – Andrew Royal Nov 15 '20 at 18:38
  • I'm including these libraries: / Are these the only ones needed – Andrew Royal Nov 15 '20 at 21:20
  • The error doesnt have anything to do with the scripts you're loading into your webpage – Redemption Okoro Nov 15 '20 at 23:28
  • As for the src(unknown) error, the browser expects the img tag to come with a non-empty src attribute, so a possible fix would be to create the img tag and append it to the body when the use selects an option – Redemption Okoro Nov 15 '20 at 23:31
  • That makes sense, but my knowledge here is lacking. How would I do that? It is so strange that in a Fiddle it works perfectly, but in application it doesn't. Would I be creating the img tag in the JQuery? – Andrew Royal Nov 16 '20 at 00:59
  • 1
    This is amazing! I am going to mark this as the correct answer. Question though, and I am hoping you can quickly answer. Is there a way that when the page loads the first selection variable shows the associated image? – Andrew Royal Nov 16 '20 at 02:06
  • I've made another update to my answer, were i implemented that. Sorry I couldn't reply quick, it was already very late in the midnight. Had to go to bed – Redemption Okoro Nov 16 '20 at 12:06
  • 1
    Thank you so much!! You have been a lifesaver. I hope that this thread gets more views along the way as I think it is an important topic with very little explanation on the web! – Andrew Royal Nov 16 '20 at 15:29
0

Your variable name should be filename not just file because that's what you're using in your code. Here's what you need.

$('select.form-control').change(function() {
   var filename = $('#shape').val() + '-' + $('#waist').val() + '.jpg';
   $('#imgToChange').prop('src', filename);
});

UPDATE

If you're pulling your images from a folder img then you need to do this:

$('#imgToChange').prop('src', 'img/'+filename);
HenryDev
  • 4,685
  • 5
  • 27
  • 64
  • @AndrewRoyal if it helped you please accept this answer as the correct one? Thanks – HenryDev Nov 14 '20 at 03:37
  • I am still struggling to get the image to output, is it something to do with my tag? – Andrew Royal Nov 14 '20 at 03:47
  • @AndrewRoyal no, using is perfectly fine. Basically, every time you select a value from one of those dropdowns the value of src attribute gets updated. For example: . Where are you pulling the images from? Are they coming from a folder? if yes, what's the folder name? – HenryDev Nov 14 '20 at 04:00
  • @AndrewRoyal if you're pulling your images from a folder then make sure to have the correct path to the src attribute – HenryDev Nov 14 '20 at 04:08
  • I am pulling from a local folder img so I'm assuming i replace 'src' with 'img/' in the prop ststement. Sorry my JQuery is limited. Ultimately the folder will be from uploads in a WordPress install – Andrew Royal Nov 14 '20 at 06:31
  • @AndrewRoyal ok, if you're pulling your images from a folder called img then do this: $('#imgToChange').prop('src', 'img/'+filename); that should work just fine. – HenryDev Nov 14 '20 at 17:56
  • Thanks @HenryDev. If I put it in a fiddle - https://jsfiddle.net/36uh2xjg/ it works perfectly, but if I run it in Chrome no dice. It won't fill in the src field. – Andrew Royal Nov 14 '20 at 20:36
  • @AndrewRoyal the answer you should just fine in any browser. That's one of the nicest things about jQuery – HenryDev Nov 15 '20 at 01:24