12

I have one image (img.png), with its content changing after a combo box changes.

The problem is the content of the image doesn't change when I change the combo box on the page.

I tried to use JavaScript like this:

function SwitchPic(pic) {
    pic.src = "img.png";
}

and the img tag is:

<img src='img.png' id='img1' name='img1'>

and the combo box tag is:

<select name="users" onchange="show(this.value);SwitchPic(img1);">
    <option value="">Select a Number:</option>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
</select>

It worked the first time I change the option, but when I re-change the option, it keeps the image without changing it.

How can I solve this?

1
  • 1
    your function doesnt select a dom element how did it even work once ? Commented Aug 31, 2014 at 0:42

4 Answers 4

12
+50

Might be just a cache problem

Instead of just using the filename, add a random part to it, like 'img.png?random', that way you'll force the browser to look for the image again on the server, but the ?random part will be discarded anyway and the file will still be found

Easiest way to do this is to add the date/time after the extension

Sign up to request clarification or add additional context in comments.

1 Comment

Easily put, you can't request image.png again, the browser will think it already has the image cached and keep on displaying the same so, instead of asking for image.png you'll ask for image.png?20140905155610 (for example), just use var d = new Date(); var image='image.png?'+d.getMilliseconds(); pic.src(image); and it should fix your problem
4

You are attempting to pass your image tag as smg1 but you don't declare that that is your image element.

try this:

SwitchPic('img1');

and then in your handler:

function SwitchPic(picID) {
  var pic = document.getElementById(picID);
  pic.src = "img.png";
}

In this scenario you pass the ID of the element you want to change then acquire the actual element inside your handler.

5 Comments

the same, the actual picture is changed,but it still the old image on the web page
what do you want to change the picture to? I just noticed that both your original image and the one you attempt to change to are both named "img.png". Are you hoping to show a picture to match the selected option in the drop down?
No, there is other program which change the picture img.png every time onchanged action happened ,, the function show() call it
Can you post the code for the show() function? I think there might be something in this function that isn't behaving the way you expect.
@user78050 what do you mean by other program change the picture img.png every time ..? And can you post show() method code?
1

I think your problem is that there is :

  1. An image by the name of img.png.
  2. A function keeps changing the actual img.png with other image but with the same name img.png
  3. You re-assign the image tag src attribute but the image doesnt refresh.

If that is the case then it is an issue of image caching of browser. refer to the links below :

How to reload/refresh an element(image) in jQuery

how to force chrome not to reload images with same url till the page is refreshed ,like firefox

How to force a web browser NOT to cache images

hope it helps!

Comments

0

Two problems:

  1. Your function only changes it to that image, rather than toggling it.
  2. You're passing a variable img1, which is not defined. You need to pass the image's ID and then use that to get the node.

So, you need to use document.getElementById() to get your image and then you need to toggle each time:

var clicked = false;

function SwitchPic(pic) {
    var image = document.getElementById(pic);
    if(!clicked){
        image.src = "otherImg.png";
        clicked = true;
    }else{
        image.src = "img.png";
        clicked = false;
    }
}

Demo

4 Comments

I have the same picture not original.png and img.png together, only img.png
@user78050 I don't know what your directory looks like so I can't reference the names of the images: original.png was just an example. Do you want to base the image off of what is selected in the dropdown?
what I'm trying to say is that I have one image
@user78050 I see what you said above to scunliffe: I agree that the show function is probably the issue here and you should post that as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.