Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate largest width of a set of elements using jQuery

I have made a quick Jsbin: http://jsbin.com/ujabew/edit#javascript,html,live

What i'm trying to achieve is to find out which is the largest <section> out of the 3 from the link. So what i'd like is to, after the loop runs, is to have var width set to the largest possible number that any of the widths could be.

Code in progress posted in link

like image 533
benhowdle89 Avatar asked Jan 13 '12 16:01

benhowdle89


People also ask

How to get width of element in jQuery?

innerWidth() - Returns the width of an element (includes padding) innerHeight() - Returns the height of an element (includes padding) outerWidth() - Returns the width of an element (includes padding and border). outerHeight() - Returns the height of an element (includes padding and border).

How to set width using jQuery?

To set the width and height of an element using jQuery, use the width() and height() in jQuery.

Which jQuery method is used to get or set the width of an HTML element?

css() is a method and width is a property name. It is one of the methods of jQuery dimension. It is used to get or set the width of the matched element. It is also used to get or set the width of the matched element.

How do you find the width of a div?

To measure the width of a div element we will utilize the offsetWidth property of JavaScript. This property of JavaScript returns an integer representing the layout width of an element and is measured in pixels. Return Value: Returns the corresponding element's layout pixel width.


1 Answers

Here:

var maxWidth = Math.max.apply( null, $( elems ).map( function () {
    return $( this ).outerWidth( true );
}).get() );

Live demo: http://jsfiddle.net/rM4UG/

like image 100
Šime Vidas Avatar answered Nov 15 '22 17:11

Šime Vidas