120
jQuery width()
jQuery width() method is used to return or set the width of matched element.
To return width: When this method is used to return the width, it returns the width of first matched element.
To set width:When this method is used to set the width, it sets the width for every matched element.
This method is one of a jQuery dimension.
List of jQuery dimension:
- width()
- height()
- innerWidth()
- innerHeight()
- outerWidth()
- outerHeight()
Syntax:
To return the width:
To set the width:
To set the width using a function:
Parameters of jQuery width() method
Parameter | Description |
---|---|
Value | It is a mandatory parameter. It is used for setting width. It specifies the width in px, em, pt etc. The default value of jQuery width() method is px. |
Function(index, currentwidth) | It is an optional parameter. It specifies a function that provides the new width of selected element.
|
Example of jQuery width() method
Let’s take an example to demonstrate the effect of jQuery width() method.
To return width:
jQuery width() example 2
To set width:
Next TopicjQuery height()