LeTung LeTung - 2 months ago 7
CSS Question

Jquery launch as soon as the page loads

I have a jquery.

$(window).on('load resize', function() {
var dm3Width = $('.dm3').width()
$('.dm1').width(dm3Width);
});


Demo Here: https://jsfiddle.net/veusr0xn/

It changes
dm1
, based on the width of
dm3
. But it runs only after I adjusted the size of the window. How does it run as soon as the page loads?

Thank so much.

Answer

Define your query under $( document ).ready(function() Scope so when document will load your query will automatically work

   $( document ).ready(function() {
     var width=$(".dm3").width();
     $(".dm1").css('width',width);
     $(window).resize(function() {
       var dm3 = $('.dm3').width();
       $(".dm1").css('width',dm3);
     });
  });