jQuery outerHeight() example

jQuery outerHeight() function is used to get the current computed outer height (including padding, border, and optionally margin) for the first element in the set of matched elements or sets the outer height of every matched element.

Example 1 - Get the outerHeight of a paragraph

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>outerHeight demo</title>
    <style>
        p {
            margin: 10px;
            padding: 5px;
            border: 2px solid #666;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>

<body>

    <p>Hello</p>
    <p></p>

    <script>
        var p = $("p").first();
        $("p").last().text(
            "outerHeight:" + p.outerHeight() +
            " , outerHeight( true ):" + p.outerHeight(true));
    </script>

</body>

</html>
Try it Yourself - Copy paste code in Online HTML Editor to see the result

Example 2 - Change the outer height of each div

Change the outer height of each div the first time it is clicked (and change its color).
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>outerHeight demo</title>
    <style>
        div {
            width: 50px;
            padding: 10px;
            height: 60px;
            float: left;
            margin: 5px;
            background: red;
            cursor: pointer;
        }

        .mod {
            background: blue;
            cursor: default;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>

<body>

    <div>d</div>
    <div>d</div>
    <div>d</div>
    <div>d</div>
    <div>d</div>

    <script>
        var modHeight = 60;
        $("div").one("click", function() {
            $(this).outerHeight(modHeight).addClass("mod");
            modHeight -= 8;
        });
    </script>

</body>

</html>
Try it Yourself - Copy paste code in Online HTML Editor to see the result

References


Comments