Donny P Donny P - 1 year ago 126
Javascript Question

Get widest element in a jQuery set

Let's say I have a bunch of

elements with different text content.

How I can I get the widest

jQuery is fine. I only care about identifying the span, not the value of the width itself.

A similar question is here. But they only get the value of the width.

Here's how I'd start:

$('span').each(function(id, value){
if ($(this).width() > w) {
largestSpan = id;

Answer Source
var maxWidth = 0;
var widestSpan = null;
var $element;
   $element = $(this);
   if($element.width() > maxWidth){
     maxWidth = $element.width();
     widestSpan = $element; 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download