Set & Get width and height Of Html Elements Using jQuery

Set & Get width and height Of Html Elements Using jQuery

In jQuery – How to set or get width and height of selected html elements. In this jQuery tutorial set & get height and width of selected html elements. You will learn how to get or set dimensions of selected html element’s using jQuery method width() & height().

Set & Get Heigth & Width

jQuery Width() method

jQuery offers various method to manipulating html elements. The jQuery width() methods get or set the width of the selected html elements.

Syntax width() Method

$("selector").width();

Using the above syntax Get width of elements

$("selector").width(value);

You can use this jquery width() syntax for Set the width of elements.

Example for jQuery Width() method

This example will demostrate, you how to set or get the width of selected HTML elements using the jQuery width () method.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get &amp; Set Width of an Element</title>
<style type="text/css">
    #div_box_body{
        width: 350px;
        height: 250px;
        padding: 28px;
        text-align: justify;
        border: 12px solid #23384E;
        background: #28BAA2;
        margin: 16px;
    }        
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_width").click(function(){
        var GetWidth = $("#div_box_body").width();
        $("#output").html("Before Set Width: " + GetWidth);
        var SetWidth = $("#div_box_body").width(500);
        $("#set_width").html("After Set - Width: " + 500);
    });
});
</script>
</head>
<body>
    <div id="div_box_body" >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla non purus nisld Dapibus nec turpis vel, semper malesuada ant.</div>
    <button type="button" id="btn_width">Get &amp; Set Width</button>
    <p id="output"></p>
    <p id="set_width"></p>
</body>
</html>                            

Output

jQuery Get & Set Width of an Element
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante.

In the above jQuery width() method example, First of all When you click on button that get selected html elements width and after that we have set width of the selected html elements and showing the current width of the box body.

jQuery Height() method

jQuery offers various method to manipulating html elements. The jQuery height() methods get or set the height of the selected html elements.

Syntax height() Method

$("selector").height();

Using the above syntax Get heigthof elements

$("selector").height(value);

You can use this jquery height() syntax for Set the height of elements.

Example for jQuery Height() method

This example will demostrate, you how to set or get the height of selected HTML elements using the jQuery height() method.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get &amp; Set Height of an Element</title>
<style type="text/css">
    #div_box_height{
        width: 350px;
        height: 250px;
        padding: 28px;
        text-align: justify;
        border: 12px solid #23384E;
        background: #28BAA2;
        margin: 16px;
    }        
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_height").click(function(){
        var heightWidth = $("#div_box_height").height();
        $("#output_height").html("Before Set Height: " + heightWidth);
        $("#div_box_height").height(300); // set the height of box
        $("#set_height").html("After Set - Height: " + 300);
    });
});
</script>
</head>
<body>
    <div id="div_box_height" >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla non purus nisld Dapibus nec turpis vel, semper malesuada ant.</div>
    <button type="button" id="btn_height">Get &amp; Set Height</button>
    <p id="output_height"></p>
    <p id="set_height"></p>
</body>
</html>                            

Output

jQuery Get & Set Height of an Element
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante.

In the above jQuery height() method example, First of all When you click on button that get selected html elements height and after that we have set height of the selected html elements and showing the current height of the box body.

Leave a Reply

Your email address will not be published. Required fields are marked *