javascript - Works in Console but not in .js file -


i trying add width , height attributes images; jquery used works in console not in linked .js file. have tried , doesn't work.

$(document).ready(function () {     $('.media-box-image').each(function () {          var $width = $('.media-box-image').width();         var $height = $('.media-box-image').height();         var $this = $(this).find('img');          $this.attr('width', $width);         $this.attr('height', $height);     }); }); 

i tried didn't work

$(document).ready(function () {     $('.media-box-image').each().on('load', function () {          var $width = $('.media-box-image').width();         var $height = $('.media-box-image').height();         var $this = $(this).find('img');          $this.attr('width', $width);         $this.attr('height', $height);     }); }); 

i checked console seems fine. else can do?

when document ready callback executes during initial page load cycle, page not have finished loading image data.

because there's no image data, images have neither widths nor heights.

use $(window).on('load'...) instead of $(document).ready(...)


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -