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. Quote Link to comment 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.. Quote Link to comment 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'> Quote Link to comment 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); } }); } Quote Link to comment Share on other sites More sharing options...
tredio Posted June 13, 2010 Share Posted June 13, 2010 thanks just what i needed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.