jQuery append img src -
how can add "_small.jpg" img src of bunch of images have in div called "banners"?
i have jquery script listening window resizes - , when below 400px want change img src of images include "_small.jpg" @ end call in small image source. so...
<img src="example.jpg" />
when viewed @ screen sizes below 480px become
<img src="example_small.jpg" />
how this?
p.s. i'd script loop through images in "banners" div update them btw :)
you might try:
// define somewhere in 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 have more "jpg" files want replace $(this).attr('src', $(this).attr('src').replace(/\.jpg/, '') + '_small.jpg'); } }); }
i believe should it!
edit - added extension remove based on comment. more information on replacing extensions see this stack.
edit 2 - added check "endswith" in case function called on items have small extension added!.
Comments
Post a Comment