Jump to content

How can I reload an image using Javascript?


latino.ad7

Recommended Posts

Your looking for a jQuery click event.

 

Assuming you've already been to jquery.com and downloaded it and included it.

 

$(document).ready(function(){
         $('.link').click(function(){
                 $('#imagecontainer').attr({'src':'urlofnewimage'});
                 return false;
         });
});

Link to comment
Share on other sites


<img src="test.png" id="myImage">

<br />

<a href="#" onclick="document.getElementById('myImage').src='somethingelse.png';">Click Me</a>

 

EDIT: I agree that jQuery is much nicer, although telling someone to go use a framework won't help them learn the underlying principles of javascript.

 

And, for latino, it is worth noting that .link will bind to any thing with a class of link, if you use that and you want different links to do different things, make sure to use # instead, just as the css selectors work, . = class # = id.

Link to comment
Share on other sites

Thanks. It works nice. :)

 

But I still have a problem. Actually 2 problems:

 

1. I have a small, thumbnail version of the same picture on the bottom of the page. I would like it to change as well. I tried adding id="myImage", but it didn't work. in other words, a single click would change 2 pictures: the main one and a small one.

 

2. I would like to have PHP variable, which would change based on what picture is displayed. This variable holds the name of a file 9different name than the picture, but which changes accordingly), which is displayed on the page in a form of a link.

Link to comment
Share on other sites

1. I have a small, thumbnail version of the same picture on the bottom of the page. I would like it to change as well. I tried adding id="myImage", but it didn't work. in other words, a single click would change 2 pictures: the main one and a small one.

 

assign it a different id. and use a js function....like so:

<script>
function reloadImages(){
document.getElementById('firstID').src='url.png';
document.getElementById('secondID').src='url2.png';
}
</script>
<a href="javascript:reloadImages();">Test</a>
<img id="firstID" src="test.png"/>
<img id="secondID"  src="test2.png"/>

 

And for your second issue: I'm more than fine helping, but i'm not going to write all your code for you. Give it a try, and post your problems in the PHP forum when you encounter them.

 

However, the way you are wording that issue (#2) makes me think you're not sure about the different between Javascript and PHP. JS is client side, PHP is server side, they happen at different times. So in order to interact between the 2, you much echo (with PHP) the data you want into JS vars, and if you want JS to give data to PHP, you must reload the page with a new url containing the info, or with AJAX

 

Link to comment
Share on other sites

Thanks. That actually solves it. I was trying to find the way not to reload the page, but just change the picture. You're right, if I want to establish some PHP variables, then the pages, where I want to do that need to be reloaded. On the other pages, I can use javascript just to change the picture.

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.