Lukas
Lukas

Reputation: 7734

Add top-margin based on height of other element with jQuery

i have element which have misc height. I need to add some margin-top to other element according as this first block. I was used for this action jQuery height property but it's does't work. Can you help me?

This is my code:

$(document).ready(function(){
    if ($('.post_discussion_header').height(79) + 'px'){
        $('.no_0').css('margin-top','110px');
        }
    });
});

TIA.

Upvotes: 4

Views: 4511

Answers (4)

user840210
user840210

Reputation:


$(document).ready(function(){
    if ($('.post_discussion_header').height() == 79) {
        $('.no_0').css('margin-top','110px');
    }
});

Upvotes: 2

kamui
kamui

Reputation: 3419

Your if statement is wrong, it should be

   if ($('.post_discussion_header').height() == 79){

however you may consider using offset height which returns the height of an element, including borders and padding if any, but not margins https://developer.mozilla.org/en/DOM/element.offsetHeight

   if (parseInt($('.post_discussion_header')[0].offsetHeight,10) == 79){ 

EDIT: added parseInt to convert the "79px" to 79 for comparison http://www.w3schools.com/jsref/jsref_parseInt.asp

Also you have one too many }); in your code. Line 5 should just be }

Upvotes: 3

mas-designs
mas-designs

Reputation: 7546

Try to work with .css("height") this should do it for you !
But don't forget if you try to add height to the actual height of the div element, jQuery.css("height") returns "23px" so you have to remove the "px" before you can add some integer value.

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129812

$('.post_discussion_header').height(79) sets the height of .post_discussion_header to 79, and returns .post_discussion_header. You then concatenate that header with 'px'. Your code will evaluate to something like

if('[object Object]px') {
    ...
}

Obviously, this is not your intention. Question is, what is your intention? What condition are you trying to check for?

You also have one set of }); too many in your code.

Upvotes: 0

Related Questions