1

Simplified, I have this structure:

<div id="RelatedPosts1">
    <div class="related-posts-thumb" style="background: url(http://xxxxxxx/s72-c/image-1.jpg)"></div>
    <div class="related-posts-thumb" style="background: url(http://xxxxxxx/s72-c/image-2.jpg)"></div>
    <div class="related-posts-thumb" style="background: url(http://xxxxxxx/s72-c/image-3.jpg)"></div>
</div>

I wish to replace the "s72-c" with "s320". Thus, changing the size of the image.

This is what I tried with jQuery:

<!-- Let's replace low-res with a higher-res -->
$(".related-posts-thumb").text(function () {
    return $(this).text().replace("s72-c", "s320"); 
});

I'm guessing this didn't work because the image URL isn't "inside" the element. So I'm not sure how to achieve my desired effect. I'm not a javascript/jQuery expert and I haven't seen any examples addressing anything similar to what I'm trying to do.

1
  • If you could provide the actual links to some example images that would help. I think I can help you do this but I need example images of each size to create an example to show you Commented Nov 26, 2014 at 0:06

2 Answers 2

1

Not really sure when you want this to trigger, so I'll just make this an on click...

 $('.related-posts-thumb').each(function() {
     $(this).attr("style", $(this).attr("style").replace("s72-c", "s320"));
 });

JSFiddle example (click on the background):

http://jsfiddle.net/wdc6ydtk/1/

Sign up to request clarification or add additional context in comments.

6 Comments

This works? That's awesome. I didn't realize you could treat the entire style of an element as a string. If you can provide an example you get a +1 from me :)
A click even isn't what I was looking for. I tried to solve the issue though using your example and I still couldn't quite get it.
This example should work fine have a look at the jsfiddle @Xarcell
This is what I used from your example to get it to work: jsfiddle.net/wdc6ydtk/2 If you update your answer I will accept it, assuming that "text" is correct way to do this.
@Xarcell Sure, I just used .html, but that should have the same effect. On load it will change the image. I have updated my answer.
|
1

You could use the .css() method with the function parameter option

$('.related-posts-thumb').css("background-image",function(idx,current){
    return current.replace('s72-c','s320');         
 });

Comments

Your Answer

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.