Calculate largest width of a set of elements using jQuery
Asked Answered
J

7

9

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

Joshia answered 13/1, 2012 at 16:13 Comment(2)
Insteading of just adding the widths, keep track of the max width found so far. var max = 0; loop( if this.width > max then max = this.width); in bad pseudo-code.Como
@MarcB That's pretty good psuedo code to me.Reversible
B
33

Here:

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

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

Batholomew answered 13/1, 2012 at 16:24 Comment(1)
Nice one. When I used this, it was for elements just inside the body that had margin: auto (for centering purposes). If you're doing something similar in IE (standards mode), remove the true being passed into the outerWidth function to remove margins from the calculation and you're golden!Parthena
K
3

Fleshing out Marc B's comment, using Math.max():

$(document).ready(function(){
  var maxWidth = 0;
  $('.content ul li').each(function(){
    var itemWidth = $(this).outerWidth(true);
    maxWidth = Math.max(maxWidth, itemWidth)
  });
});
Kassa answered 13/1, 2012 at 16:24 Comment(0)
A
0

I believe you want to look into the underscore library. Specifically, the max method

Aftershock answered 13/1, 2012 at 16:17 Comment(0)
C
0
$(document).ready(function(){
  var maxWidth = 0;
  $('section').each(function(){
    w = $(this).outerWidth(true);      
    if ( w > maxWidth)
            maxWidth = w;
  });
});
Creepy answered 13/1, 2012 at 16:23 Comment(0)
M
0

Change the .each() loop to this:

var thisWidth = $(this).outerWidth(true);

if (thisWidth > width) {
    width = thisWidth;
}
Maintenon answered 13/1, 2012 at 16:25 Comment(0)
N
0

This was my idea:

$(document).ready(function () {
  var elements = $(".content ul li");
  var count = elements.length - 1;
  var width = [];

  elements.each(function (n) {
    width.push($(this).outerWidth(true));
    if (count == n) {
      width = Math.max.apply(Math, width);
    }
  });
});

I added the count of the number of elements and then runs the Math.max to get the largest number when each loop is done.

Neologism answered 13/1, 2012 at 17:3 Comment(0)
N
0

I have just written a function regarding this. I thought it might help others. (jQuery)

$.fn.largerWidth = function () {    //function(p)  
                                    // where 'p' can be clientWidth or offsetWidth 
                                    // or anything else related to width or height
    var s = this,m;
    for (var i = 0; i < s.length; i++) {
        var w = s[i].offsetWidth;   // s[i][p]; 
        (!m || w > m) ? (m = w) : null
    }
    return m;
}
Nuclide answered 9/5, 2013 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.