jQuery is a JavaScript library that provides multiple features and tools to handle the HTML document. To use jQuery, we must include its library in the HTML document using the following code:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
outerWidth()
methodThe outerWidth()
method in jQuery helps us deal with an element's outer width. The outer width of an element is not a CSS property; it is a concept that refers to the sum of an element's width, horizontal border, horizontal padding, and horizontal margin (optional).
Note: The
outerwidth()
method is not applicable towindow
anddocument
objects. We usewidth()
method instead.To study more on the
width()
method we can refer to this Answer.
outerWidth()
methodIt is used for two purposes:
To get the outer width of the first element in our selected elements
To set the outer width of all elements in our selected elements
Let's discuss each purpose now.
The outerWidth()
method helps us get the outer width of the first element in our selected elements.
Following is the syntax to use the outerWidth()
method for the aforementioned purpose.
$(selector).outerWidth(includeMargin);
$selector
: The element we want to obtain the outer width of.
includeMargin
(optional): The boolean variable we set true
if we want to include the margin in the outer width. By default, its value is false
.
The following code demonstrates how we can use the outerWidth()
method to get the outer width of the first element in a selected set of elements.
Lines 4–7: We define the document's title
and include the jQuery library in the <head>
tag.
Lines 9–24: We write the styling for our two divs: myDiv1
and myDiv2
. We apply margin, padding, and border for both the divs so when we calculate the outer width, we can verify if these values are present in our result.
Note: The
document.ready()
function executes after the DOM has loaded completely without making an explicit call.
Lines 28–31: We call the outerWidth()
method for our elements with the class Div
when the button widthBtn
is clicked. The first element having this class name will be selected. To verify our results, we set our widthResult
text's value to the retrieved value of our outerWidth()
function. The parameter in the outerWidth()
method means that we want to include margin in our result.
Lines 35–40: We add two divs with the id myDiv1
and myDiv2
, a button with the id widthBtn
, and a paragraph element with the id widthResult
.
The outerWidth()
method helps us set the outer width of multiple elements which match our selector. There are two ways we can use the method to do so.
Here, we send a value as a parameter, and our selected elements' current outer width is changed to the newOuterWidth
we send as a parameter. Following is the syntax to use the outerWidth()
method for the aforementioned purpose.
$(selector).outerWidth(newOuterWidth);
$selector
: The element we want to set the outer width for.
newInnerWidth
: The new outer width we want to set for all matched elements. It can either be a number or a string containing the units. If we pass a number, the outer width is considered to have a pixel unit.
includeMargin
(optional): The boolean variable we set true
if we want to consider margin inclusive while setting the outer width for the elements. By default, its value is false
.
The following code demonstrates how we can use the outerWidth()
method to set the outer width of all selected sets of elements using a value as a parameter.
Note: Setting the outer width in jQuery modifies the width of the element keeping the padding, margin and border constant that was specified in the styling.
Lines 29–31: When the button with id widthBtn
is clicked, we call the outerWidth()
method to set the outer width of all elements with the class Div
as 500,
which we send as a parameter. For myDiv1
, the width will become 458px while right padding, left padding, right margin, and left margin will be 10px each. The border on the right and left will be 1px each.
Lines 35–39: We add two divs with the id myDiv1
and myDiv2
and a button with the id widthBtn
.
Here we send a function as a parameter, and our selected elements' outer width is changed in the function. We use this method if we want to perform some additional operations or modifications on the current outer width of our selected elements. Following is the syntax to use the outerWidth()
method for the aforementioned purpose.
$(selector).outerWidth(function(currentIndex, currentOuterWidth) {// Performing operations or modifications on currentOuterWidthreturn newOuterWidth;});
$selector
: The element we want to set the outer width for.
currentIndex
: The current index of the element we want to select within the selected set.
currentOuterWidth
: The current outer width of the element we want to select within the selected set.
newInnerWidth
: The new outer width we want to set for our elements. Our outerWidth()
method sets our element's current outer width to the returned value.
The following code demonstrates how we can use the outerWidth()
method to set the outer width of all selected sets of elements using a function as a parameter.
Lines 29–34: When the button with the id widthBtn
is clicked, we call the outerWidth()
method with a function as a parameter. In this function, we modify the currentOuterWidth
by adding additional 200
pixels to each element's currentOuterWidth
and storing the result in newOuterWidth
. This newOuterWidth
is returned by the function which sets our elements' outer width to it.
The outerWidth()
method allows us to retrieve and manipulate the outer width of HTML elements. It allows us to retrieve and set the outer width of our elements effortlessly.
Free Resources