schilly Posted May 25, 2009 Share Posted May 25, 2009 is there an easy way to change an image src and reload it with jquery? thx. Link to comment https://forums.phpfreaks.com/topic/159622-solved-jquery-reload-an-image/ Share on other sites More sharing options...
DarkSuperHero Posted May 25, 2009 Share Posted May 25, 2009 $(function() { //the following selector is for the image you are trying to select/change the src... $('.nav a img').hover( function() { this.src = 'new.jpg'; }, function() { this.src = 'old.jpg'; } ); }); and ironically.... http://lmgtfy.com/?q=jquery+img first result on google... :-) I hope this is what you were looking for.. Link to comment https://forums.phpfreaks.com/topic/159622-solved-jquery-reload-an-image/#findComment-841920 Share on other sites More sharing options...
schilly Posted May 25, 2009 Author Share Posted May 25, 2009 no it's a little different that just a hover. i am creating thumbnails from a video file and if the thumbnail taken from the video is in an undesirable location then i am clicking a button, using ajax to call the script to generate a new image then i just need the existing image to refresh and show the new image. currently have: function reloadPic(thumb,video,aspect,id){ var string = 'thumb='+thumb+'&video='+video+'&aspect='+aspect; var imageID = '#' + id; $.ajax({ type: "GET", url: "newThumb.php", data: string, success: function(msg){ var img_src = $(imageID).attr('src'); $(imageID).attr('src',img_src); } }); } with the link being: <input type='button' onClick=\"reloadPic('$thumb1','$movie1','$aspect','$tutorial"."1');\" value='Reload Thumb'> Link to comment https://forums.phpfreaks.com/topic/159622-solved-jquery-reload-an-image/#findComment-841940 Share on other sites More sharing options...
schilly Posted May 26, 2009 Author Share Posted May 26, 2009 ok got it working. when reloading from the same url, the browser would always pull from cache so even though the image was different, it would just use the cached version. so i found a hack about adding a query string on an image src to force it to reload instead of using the cached version. function reloadPic(thumb,video,aspect,id){ var string = 'thumb='+thumb+'&video='+video+'&aspect='+aspect; var imageID = '#' + id; $.ajax({ type: "GET", url: "newThumb.php", data: string, success: function(msg){ var img_src = $(imageID).attr('src'); var timestamp = new Date().getTime(); $(imageID).attr('src',img_src+'?'+timestamp); } }); } Link to comment https://forums.phpfreaks.com/topic/159622-solved-jquery-reload-an-image/#findComment-841979 Share on other sites More sharing options...
tredio Posted June 13, 2010 Share Posted June 13, 2010 thanks just what i needed Link to comment https://forums.phpfreaks.com/topic/159622-solved-jquery-reload-an-image/#findComment-1071622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.