Home >>jQuery Tutorial >jQuery width() Method

jQuery width() Method

jQuery width() Method

jQuery width() method in jQuery is used to returns the width of the selected FIRST matched elements. This method does not include margin, padding or border.

Return the width: $(selector).width()
Set the width: $(selector).width(value)
Set the width using a function: $(selector).width(function(index,currentwidth))

Parameter Values

Parameter Description
value It is used to require for setting the width. Specifies the width in em, px, pt, etc. Its default unit is px
function(index,currentwidth) It is optional parameter and is used to returns the new width of selected elements
  • index – Used to returns the index position of the element
  • currentwidth – Used to returns the current width of the selected element
Here is an Example of jQuery width() Method:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
alert("Width of div: " + $(".WiDth").width());
<div class="WiDth" style="height:80px;width:250px;padding:8px;margin:2px;border:1px solid yellow;background-color:#f33003;"></div><br>
<button class="btn1">Click me to Display the width</button>