kliyo35
kliyo35

Reputation: 3

jQuery create an element and get height

I created a div in jquery whose width and height set automatically and I would like to assign the height value to line-height.

$('<div>')
    .css({
        'position': 'absolute',
        'left': 0, 'right': 0, 'top': 0, 'bottom': 0,
        'line-height': $(this).height()
    });

But $(this) is the body element so the line-height is the height of body.

Someone has a solution?

Thanks in advance!

Upvotes: 0

Views: 186

Answers (1)

Ram
Ram

Reputation: 144739

You can use the callback function. css method will call the function, the returned value of the function is used for setting the property's value:

$collection.css({
    'position': 'absolute',
    'left': 0, 'right': 0, 'top': 0, 'bottom': 0,
    'line-height': function() {
        return $(this).height() + "px";
    }
});

Note that as you haven't added the element to the document the div element's height is 0, you should call the css method after appending the element.

Upvotes: 2

Related Questions