The width() is an inbuilt function in JavaScript which is used to check the width of an element. It does not check the padding, border and margin of the element.
Syntax:
$("param").width()
Parameters: Here parameter is “param” which is the class or id of the element whose width is to be extracted.
Return values: It returns the width of the selected element.
Code #1:
<html> <head> /3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { var msg = ""; msg += "Width of div: " + $("#demo").width(); $("#demo").html(msg); }); }); </script> <style> #demo { height: 150px; width: 350px; padding: 10px; margin: 3px; border: 1px solid blue; background-color: lightgreen; } </style> </head> <body> <div id="demo"></div> <button>Click Me!!!</button> <p>Click on the button and check the width of the element (excluding padding).</p> </body> </html> |
Output:
Before clicking on the button-

After clicking on the button-

jQuery also include innerWidth() method i.e, it also used to check inner width of the element including padding.
Syntax:
$("param").innerWidth()
Parameters: Here parameter “param” is the class or id of the element whose width to be extracted.
Return value: It returns the width of the selected element.
Code #2:
<html> <head> jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { var msg = ""; msg += "Inner width of div: " + $("#demo") .innerWidth() + "</br>"; $("#demo").html(msg); }); }); </script> </head> <style> #demo { height: 150px; width: 350px; padding: 10px; margin: 3px; border: 1px solid blue; background-color: lightgreen; } </style> <body> <div id="demo"></div> <button>Click Me!!!</button> <p>Click on the button and check the innerWidth of an element(includes padding).</p> </body> </html> |
Before clicking on the button-

After clicking on the button-

Recommended Posts:
- How to animate div width and height on mouse hover using jQuery ?
- How to get the width of hidden element in jQuery ?
- How to dynamically set the height and width of a div element using jQuery ?
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- How to get the image size (height & width) using JavaScript ?
- p5.js | width variable
- How to get the width and height of an image ?
- How to find the width of a div using vanilla JavaScript?
- How to get the width of device screen in JavaScript ?
- Calculate the width of the text in JavaScript
- How to get the width of scroll bar using JavaScript ?
- How to calculate Width and Height of the Window using JavaScript?
- How to Adjust the Width of Input Field Automatically using JavaScript ?
- How to add stroke width of text canvas using Fabric.js ?
- How to set stroke width of a canvas circle using Fabric.js ?
- Fabric.js | Circle width Property
- Fabric.js | Triangle width Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


