Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add height to existing height?

For example I want to add 20px to <div id='example'></div>, which is currently 20px.

I could get the existing height and add 20px and input it as the new height, but I wish to learn if there is a better way that might work like a += operator.

like image 778
whamsicore Avatar asked Jan 02 '12 10:01

whamsicore


2 Answers

There are many ways to do it: http://jsperf.com/jquery-height-vs-css-height

jsbin.com/utaduy

$('#example').css( "height", "+=20px" );

$('#example').height( $("#example").height() + 20 );
like image 181
Andreas Louv Avatar answered Oct 06 '22 00:10

Andreas Louv


You can pass a function into height() that has the element's current height as an argument and the return of the function will be the new height:

$('#example').height(function (index, height) {
    return (height + 20);
});

Here is a demo: http://jsfiddle.net/grajh/

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

like image 35
Jasper Avatar answered Oct 05 '22 23:10

Jasper