jQuery Tutorial Tutorials - jQuery .width() method

jQuery .width() method

The .width() method in jQuery is used to get/set the width of the matched element.

jQuery .width() method Syntax

.width()
.width(value)
.width(function)
Parameter Type Description
value String,Number the width value to set
funtion(index, oldwidth) Function a function returning the width to set.
index is the position of the element in the set.(based on 0)
oldwidth is the old width.

.width()

Get the width of the first matched element.(If the element is not visible, the width would be 0 )

example

Get the width of the first paragraph

$("p").width();

Try now

.width(value)

example

Set the width of each p to 300px

$("p").width(300);

Try now

.width(function)

Set the element 20px higher than the previous.

$("p").width(function(index,oldValue){
    return 20*index + oldValue;
});

Try now

Date:2019-08-20 15:55:45 From:www.Lautturi.com author:Lautturi