Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know text is breaking using javascript or jquery

I have table in some colums text is breaking in two lines. I want to reduce font size for which td text is breaking. I am not getting any clue to where to start coding for this issue http://jsfiddle.net/WZNPx/

<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>sdfasdfas asdfasf asdfsaf </td>
<td>adfafdafd adfasdf afasdf sdfsadfasasdfa sdf asdfsafd adfaf</td>
<td>adfasf</td>
</tr>
</table>
like image 468
Jitender Avatar asked Sep 10 '26 02:09

Jitender


1 Answers

Please find Fiddle here: http://jsfiddle.net/WZNPx/7/

The code is fairly simple:

var tds = $("#test td")

for (var i = 0, l = tds.length; i < l; i++)
{
    var td = tds.eq(i);
    td.prepend("<span class='testerStart'></span>");
    td.append("<span class='testerEnd'></span>");
    var s = $(td).find(".testerStart").position().top;
    var e = $(td).find(".testerEnd").position().top;
    console.log(s,e);
    if (e > s) console.log("Multiple lines");
    else console.log("Single line");
}

The idea is to insert empty span at the beginning and at the end of the text and check if their y positions are different.

For some reason it doesn't work when the text ends with space.

like image 155
strah Avatar answered Sep 12 '26 14:09

strah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!