Here is a jQuery code that I am using to expand a photo, as it expands I want to replace the thumbnail version of the image to the original sized version.
$('img.photo_share_image').click(function() {
var new_image = $(this).replace(/thumb_/ig, $(this).attr("src"));
$(this).animate({width:'100%'},500);
})
The original image is put onto the page by the HTML tag. It is sourced from http://www.example.com/thumb_someimage.jpg what I need to do is remove the thumb_ from the source url.
I have already tried this by using this code
var new_image = $(this).replace(/thumb_/ig, $(this).attr("src"));
but it didn't work. Any suggestions?
-
replace('/thumb/imagename.jpg', $(this).attr('src')); If IG is a variable containing the current imagename use replace('/thumb/'+ig, $(this).attr('src'));Jonas m– Jonas m2012年01月25日 17:21:53 +00:00Commented Jan 25, 2012 at 17:21
-
@Jonas m : ig is not a variable but regex flags.Kumsal Obuz– Kumsal Obuz2012年01月25日 17:38:10 +00:00Commented Jan 25, 2012 at 17:38
-
Ahh suddently it all made sense ;)Jonas m– Jonas m2012年01月25日 19:55:20 +00:00Commented Jan 25, 2012 at 19:55
2 Answers 2
use this
var new_image = $(this).attr('src', $(this).attr('src').replace(/thumb_/ig, ""));
Moreover, you may want to wait for your image to finish loading if it's a big one because your animation will start right after you assign a new source for your image. So, animation will not necessarily wait for it but work on the existing image DOM element.
2 Comments
.replace I was going to try with replaceWith() method.Right now, you're calling $(this).replace but $(this) appears to be an img element. You'd need to call replace() on $(this).attr("src"), no?