fightstarr20
fightstarr20

Reputation: 12598

jQuery get width of div on resize

I am trying to get the width of a div on resize and show it in the console like this..

function matchHeight() {
    var newHeight = jQuery("#container").height(); //where #grownDiv is what's growing
	console.log(newHeight);
}


jQuery(window).on('resize', function(){
matchHeight();
});	
#container{
  background:tan;
  width:100%;
  height:500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
Container Content
</div>

It is not refreshing the value, where am I going wrong?

Upvotes: 1

Views: 58

Answers (2)

Nick Tiberi
Nick Tiberi

Reputation: 1141

You want the width, but you're calling .height(). Try var newWidth= jQuery("#container").width();

Upvotes: 1

Spartacus
Spartacus

Reputation: 1508

Probably because you want to grab the width, and instead you're grabbing the height... which doesn't change on resize.

function matchWidth() {
    var newWidth = jQuery("#container").width(); //where #grownDiv is what's growing
    console.log(newWidth);
}


jQuery(window).on('resize', function(){
    matchWidth();
});

Upvotes: 3

Related Questions