width() method in jQuery

width() in jQuery

This method is used to get the current width of the first element in the set of matched elements.

This method does not include margin, border or padding.

Syntax 1 - Get the current width of the first element.

$(selector).width()

Syntax 2

$(selector).width(value)

  • value - An integer representing the number of pixels
Syntax 3

$(selector).width(function(index,currentWidth))

  • index - index position of the element in the set.
  • currentWidth - old width of the element.

Example 1 - get

Try Yourself

Example 2 - set

Try Yourself

Example 3 - with function

Try Yourself

Example 4 - Difference between width() and innerWidth()

Try Yourself

Example 5 - Difference between width() and outerWidth()

Try Yourself