1

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?

asked Jan 25, 2012 at 17:19
3
  • replace('/thumb/imagename.jpg', $(this).attr('src')); If IG is a variable containing the current imagename use replace('/thumb/'+ig, $(this).attr('src')); Commented Jan 25, 2012 at 17:21
  • @Jonas m : ig is not a variable but regex flags. Commented Jan 25, 2012 at 17:38
  • Ahh suddently it all made sense ;) Commented Jan 25, 2012 at 19:55

2 Answers 2

4

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.

Sarfraz
384k81 gold badges561 silver badges613 bronze badges
answered Jan 25, 2012 at 17:22
Sign up to request clarification or add additional context in comments.

2 Comments

I dint knew/remember that theres a method .replace I was going to try with replaceWith() method.
replaceWith is jQuery. What we are trying to do here is to get the string value with attr('src') then use Javascript replace function. We are using regular expression but in this case, it's a bit overkill. We could also simply use replace('thumb_','') since we know the image src attribute format. There is no need for flags like /i and /g
1

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?

answered Jan 25, 2012 at 17:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.