Dean Elliott
Dean Elliott

Reputation: 729

jQuery, get width of one element and apply to another

Is there a way that I can get the width of one element .container for example and apply it to another .item for example?

My layout is responsive, hence why I can't just give .item a fixed width value.

Upvotes: 4

Views: 3431

Answers (4)

kdureidy
kdureidy

Reputation: 960

To get the width without jquery, just simple javascript line of code:

var width = document.getElementById('foo').offsetWidth;

Note: this will get the width of the element + the border width as well

Ex. html:

<div class="foo"></div>

style:

.foo {
  width: 100px;
  border: 1px solid #CCC;
}

in this case the width will be:

100px width + 1px right border + 1px left border = 102px

Hope that help.

Upvotes: -1

Adil Shaikh
Adil Shaikh

Reputation: 44740

Like this -

$('.item').width($('.container').width());

Demo ----> http://jsfiddle.net/xQ7hh/

Upvotes: 12

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Try this:

 $('.item').width($('.container').width())

Upvotes: 3

Wallack
Wallack

Reputation: 792

Try:

$(".item").width($(".container").width());

Upvotes: 2

Related Questions