jQuery width() Method
jQuery width() method sets or returns the width of selected elements.
The width() method is useful when an element's width needs to be used in a mathematical calculation.
The width() method is also able to find the width of the window and the document.
Syntax
Here is a syntax for width() method that return the width
$(selector).width();
Set the width
$(selector).width( value );
Parameter | Type | Description |
---|---|---|
value | String | Optional. Specifies the width in px, em, pt, etc. Default measurement is px |
Example: Get width
$(document).ready(function(){
$("button").click(function(){
alert($("p").width());
});
});
Example: Set Width
$(document).ready(function(){
$("button").click(function(){
$("p").width("200px");
});
});