javascript - img.height() and img.width() returning 0 in chrome,but returns correct value in mozilla -
i have image field in markup.i setting source using jquery. need height , width. have used image.height() , image.width() methods. works proper in mozilla not in chrome, have tried image.height , image.width method, not getting value. have tried changing prop method attr setting image src, gives me same result. pasting code here.
html
<div> <img src class="my-image"/> </div>
jquery
$(document).ready(function(){ var img = $(".my-image"); imagewidth = $('.my-image').width(); imageheight = $('my-image').height(); alert(imageheight); });
what error here ? appreciated
what happens image not yet loaded on $(document).ready();
, @ point element has dimensions of 0px
0px
. in order ensure image loaded use $(window).load();
event instead of document ready.
$(window).on("load", function(){ var img = $(".my-image"), imagewidth = $('.product-image-og-size').width(), imageheight = $('img.product-image-og-size').height(); alert(imageheight); });
Comments
Post a Comment