Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to get elements height value and use it in another element via css code?

I want to get height ( or width.. doesnt really matter ) from element A and use that height to set css to element B ...like

$('#element_B').css({ 'top','element_A_height' })

A little example here.. I know the javascript is not even close to something that works, I just tried to explain what i wanted to happen..

http://jsfiddle.net/cJdXg/

I have no idea how to achieve this.. any ideas?

like image 621
Joonas Avatar asked Dec 13 '22 13:12

Joonas


2 Answers

Here's an example of what you want to achieve: http://jsfiddle.net/cJdXg/2/

HTML:

<div id="BoxeeBox">Lorem ipsum dolor. #boxeeBox</div>
<div id="emptyBox1">#emptyBox1</div>
<div id="emptyBox2">#emptyBox2</div>

JavaScript:

var BoxeeBox = $('#BoxeeBox'); /* cache the selector */

$('#emptyBox1').css({ width: BoxeeBox.width() });
$('#emptyBox2').css({ height: BoxeeBox.height() });

CSS:

div { background: #b6d754; color: #ffffff; padding: 10px; margin: 5px; float: left; clear: both; border-radius:10px; }
like image 115
amustill Avatar answered Jan 31 '23 07:01

amustill


What you're after is this:

http://api.jquery.com/height/

Example:

var elementAheight = $('#elementA').height();

however, if elementA has margins you may want to us this:

http://api.jquery.com/outerHeight/

Example:

var elementAoutheight = $('#elementA').outerHeight();

Hope it helps!

like image 25
udjamaflip Avatar answered Jan 31 '23 06:01

udjamaflip