Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the width of a text node

A bit of (JS-modified) HTML might look like this:

<div style="width: 50px;">1,234,567</div>

How do I detect if the text node is wider than the container?

like image 251
Niet the Dark Absol Avatar asked Oct 06 '22 22:10

Niet the Dark Absol


1 Answers

As inspired by How to detect overflow in div element?:

<!-- Overflowing -->
<div style="width: 50px;">1,234,567</div>

<!-- Not overflowing -->
<div>1,234,567</div>

JavaScript (no jQuery) to detect:

var divs = document.getElementsByTagName('div');
var i, div, overflowing;

for (i=0; i<divs.length; i++) {
    div = divs[i];
    overflowing = div.offsetWidth < div.scrollWidth;
    console.log(overflowing);
}​

http://jsfiddle.net/mattball/pYj5P/

like image 128
Matt Ball Avatar answered Oct 11 '22 01:10

Matt Ball