Swati Maheshwari Swati Maheshwari - 6 months ago 13
Javascript Question

How to find rectangle with maximum width in javascript?

How to find rectangle with maximum width in javascript?

rectangle is not having any id/ name.
I wonder if any such way exists!

Answer

Supposing your html is like this:

<div class="rectangle" name="r1">
  <table>
    ...
  </table>
</div>
<div class="rectangle" name="r2">
  <table>
    ...
  </table>
</div>

then you could do something like this:

var widthMax = 0;
var nameMax;
$('.rectangle').each(function(i, obj) {
  var width = obj.offsetWidth;
  if (width > widthMax) {
    nameMax = obj.name;
  }
});
console.log('the name of the rectangle with the maximum width is', nameMax);
Comments