0

I have two div's. #divA & #divB.

#divA has a width of 100% and I want to get the px value of the div and apply it to #divB as it's height CSS. I've tried using .offsetWidth but to no avail. Can anybody help me?

50dollanote
  • 189
  • 4
  • 4
  • 12

2 Answers2

2

use .css('width') to keep units intact (ex: 400px), use .width() to get just the numerical value (ex: 400) for use in mathematical computations.

for what you're doing:

var divAWidth = $('#divA').css('width');
$('#divB').css('height', divAWidth);

if you're getting mathematical:

var divAWidthNumber = $('#divA').width();
var halfThatWidth = divAWidthNumber / 2;
erikrunia
  • 2,371
  • 1
  • 15
  • 22
  • Excellent! Thank you! I have it working, just wondering if it's possible to implement to a style sheet rather than a page? – 50dollanote Jan 11 '14 at 15:37
  • 1
    If you mean assign the width to the height via CSS instead of JavaScript i would say no, but I'm sure somebody smarter may chime in and school us both. :-) – erikrunia Jan 11 '14 at 15:43
1

If you don't want to use jQuery:

var getwidth = document.getElementById("div1").style.width;
document.getElementById("div").style.height = getWidth;

With jQuery

var getwidth = $("#div1").width();

EDIT

$("#div").height(getwidth);
mkaminsky
  • 353
  • 2
  • 10