0

How can I add "_small.jpg" to the img src of a bunch of images I have in a div called "banners"?

I have a jQuery script listening for window resizes - and when below 400px I want to change the img src of the images to include "_small.jpg" at the end to call in the small image source. So...

<img src="example.jpg" />

when viewed at screen sizes below 480px would then become

<img src="example_small.jpg" />

How would I do this?

P.S. I'd like the script to loop through all the images in the "banners" div to update them btw :)

3
  • 4
    I think this is not a nice move, because if you have the src set to example.jpg in your html, those images will be loaded anyway and then replaced when you add the _small string. I would either leave the src empty and load all the images with js (depending on the screen size) or set the size of the images in your css. Commented Mar 10, 2014 at 15:42
  • Instead keep one image and give max-width:100% Commented Mar 10, 2014 at 15:43
  • I want to load a different image with different aspect ratio for small use cases on the carousel - hence the question - keeping the same source at 100% is easy - but means you have a carousel which looks nice on desktop and crap on mobile if you use the same img src scaled to the width whilst maintaining aspect ratio :) Commented Mar 10, 2014 at 17:17

3 Answers 3

1

You can do:

$("div.banners img").each(function() {
    $(this).attr("src", function() {
        var src = this.src.split(".")[0];
        return src + "_small.jpg";
    });
});

(The above assumes image names have no "." in them and all have a type of JPEG)

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

1 Comment

You don't need to iterate through the images, $('div.banners img').attr should be enough jsfiddle.net/Alfie/SrH8X
0

You might try:

// define this somewhere in your code (credit: https://stackoverflow.com/questions/280634/endswith-in-javascript)
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

if ($(window).width() < 480) {
  $("div.banners img").each(function() {
    if (!endsWith($(this).attr('src'), '_small.jpg')) {
      // if you have more than "jpg" files you will want to replace those as well
      $(this).attr('src', $(this).attr('src').replace(/\.jpg/, '') + '_small.jpg');
    }
  });
}

I believe that should do it!

Edit - Added an extension remove based on a comment. For more information on replacing extensions see this stack.

Edit 2 - Added a check for "endswith" in case the function is called on items that already have the small extension added!.

9 Comments

@isherwood You are correct! I think that should do it now.
@dubbs Should be fixed. Sorry about that!
done. a nice solution, but the issue here is when using this on a fluid responsive approach, i am testing to check the window size on any change to the browser size which then re-does this script - therefore adds _small.jpg again... which breaks it... any way of only making it add _small.jpg if not already done so?
@dubbs I added a function call to "endsWith" to make sure that the image source doesn't have "_small.jpg" already. Best of luck!
@drew_w - thanks - but that doesnt seem to work... on resize, the script is still firing and adding _small to the JPEGs...
|
0

You can try with:

var src = $('img').attr('src'),
    ext = src.substr( src.lastIndexOf('.') );

src = src.replace(ext, '_small' + ext, src);

$('img').attr('src', src);

Or with regex:

var src = $('img').attr('src'),
    src = src.replace(/(\..*)$/, '_small$1', src);

$('img').attr('src', src);

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.