jquery - Switching img src at certain viewport -


completely stumped why not working. trying switch image source when viewport less 500px, silly doing wrong can't see it.

    $(document).ready(function () {   var width = $(window).width();  if (width <= 500) {  $("#my-image").attr("src","images/img-2.png"); } else { $("#my-image").attr("src","images/img-1.png"); }  });  <div>    <img id="my-image">  </div> 

here's full example of using resize()

$(document).ready(function () {      var img = $("#my-image");      var $window = $(window).on('resize', function(){         var width = $window.width();          if (width <= 500) {             img.attr("src","images/img-2.png");         } else {             img.attr("src","images/img-1.png");         }     }); }); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -