1

I have this HTML

<div class="picture"></div>

and depending on the viewport width, I'd like to add either a small or a large image to the div, so that it will look like this:

<div class="picture"><img src="small.jpg></div>

Here's some jQuery I found somewhere on the web which seems to do exactly what I want. The only part where I'm stuck now is the img src="....jpg" thing. How can I translate this into jQuery?

jQuery(document).ready(function() {
    $(window).resize(function() {
        var window_width = $(document).width();
        if (window_width <= 1024) {

            img src = "small.jpg"

        } else {

            img src = "large.jpg"

        }
    });
});

Thanks!

Sushil
  • 2,837
  • 4
  • 21
  • 29
Groen91
  • 95
  • 7

7 Answers7

2

If you are creating a <img>

$(window).resize(function() {
    var window_width = $(document).width();
    if (window_width <= 1024) {
        $(".picture").html("<img src='small.jpg'>");
    } else {
        $(".picture").html("<img src='large.jpg'>");
    }
});

or if you just want to assign a source,

$(window).resize(function() {
    var window_width = $(document).width();
    if (window_width <= 1024) {
        $(".picture").find("img").attr("src", "small.jpg");
    } else {
        $(".picture").find("img").attr("src", "large.jpg");
    }
});
Varun
  • 1,946
  • 2
  • 11
  • 17
  • Thanks, I think the first one is what I'm looking for. Will test it now and report :) – Groen91 Jun 23 '15 at 18:34
  • Ok, this works fine, thanks again! The only strange thing now is when I load the page, both images don't show up, only when I change my browser size. Is there a simple way to fix it, so that they show up on default? – Groen91 Jun 23 '15 at 18:46
  • @Groen91 You want both the images on load or any specific one? – Varun Jun 23 '15 at 18:53
  • Yes, the specific one, not both. – Groen91 Jun 23 '15 at 18:58
  • 1
    @Groen91 I guess it would be the same code,but in `$(document).ready()` ,better put it inside a function and call it on `load` as well as on `scroll`. – Varun Jun 23 '15 at 19:00
2

You can check if div has img element updated its src else create new img element and append it.

jQuery(document).ready(function() {
    $(window).resize(function() {
        var window_width = $(document).width();
        var imgsrc = "large.jpg"
        if (window_width <= 1024) {
            imgsrc = "small.jpg"
        }
        //Picture element
        var picture = $('.picture');

        //Picture has img element update its src else append new img tag
        if (picture.find('img').length) {
            picture.find('img').attr('src', imgsrc );
        } else {
            picture.append('<img src="' + imgsrc + '" />')
        }
    });
});
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

Try this code snippet:

jQuery(document).ready(function () {
    $(window).resize(function () {
        var window_width = $(document).width();
        var src = (window_width <= 1024) ? "small.jpg" : "large.jpg";
        $('.image').remove(); //Remove in order to avoid multiple images to be added.
        $('.picture').prepend($('<img />', {src: src, class: 'image'}));
    });
});
taxicala
  • 21,408
  • 7
  • 37
  • 66
0

Like this :

$('.picture').find('img').attr('src','small.jpg');
Alexandre
  • 1,940
  • 1
  • 14
  • 21
0

You'll need to update the attributes.

In jQuery, you'll do something like this to update the src:

$('<img>') // create a new img node
  .attr('src', 'newsrc')  // set the src to your desired source
  .load(function(){
     this.appendTo('.picture');  // once the source has finished loading, append it to the <div>
   });

For more information on this, check out this other Stack Overflow post:

Changing the image source using jQuery

Community
  • 1
  • 1
Danny Delott
  • 6,756
  • 3
  • 33
  • 57
0

In jquery you can use append

$('.picture').append('<img src="small.png"/>'); 
meteor
  • 2,518
  • 4
  • 38
  • 52
0

Cache both images up front in jQuery objects constructed with image elements pointing to their source.

Then, use your conditional statement to store the string for source and reference it to place the cached image element on the page.

jQuery(document).ready(function() {
    //cache images
    var imgSet = {
        "small.jpg" : $('<img src="small.jpg" />'),
        "large.jpg" : $('<img src="large.jpg" />')
    };
    $(window).resize(function() {
        var window_width = $(document).width();
        var imgsrc;
        if (window_width <= 1024) {
            imgsrc="small.jpg";
        }else {
            imgsrc="large.jpg"
        }
        //use cached image element
        $('.picture').empty().append(imgSet[imgsrc]);
   });
});
Travis J
  • 81,153
  • 41
  • 202
  • 273