JavaScript Add a br tag in between of the text (ellipsis in second line)

First of all, you should check out Rob's answer. JS is a really extreme mean to solve these sort of issues.


I wrote a short utility function for you that does what you want, it will look cut the first line of al emenet an add a <br> after it. This will let you keep ellipsis on the second line.

Here is generally how it works:

  • Create a mock element
  • Start writing to it one letter at a time
  • Stop right before its width is bigger than your element
  • Set the HTML of the element to the text you matched + <br /> + the rest of the text

Here is the code:

function appendBR(element){
    var text = element.textContent.split("");
    var width = element.offsetWidth;
    var el = document.createElement("el");
    document.body.appendChild(el);
    while(text.length > 0){
        el.textContent+=text.shift();
        if(el.offsetWidth > width-20){
            element.innerHTML = el.textContent+"<br />"+text.join("");
            document.body.removeChild(el);
            return;
        }
    }    
    document.body.removeChild(el);
}

var container = document.getElementById("first");
appendBR(container);

Working fiddle

Note, if you want it to be resistant to changing the size of the window, you can add a listener (using addEventListener and shimming with attachEvent) to scroll. Here is a working fiddle for that.

Answer:1

You'd better use this for ellipsis if you can:

width:80px;
overflow:hidden; 
text-overflow:ellipsis;
white-space:nowrap;

This will automatically add ... to the end of string if there's not enough space.

Answer:2

Consider: var success = function(data){ data = process_data(data); console.log(data); }; vs. var success = function(data){ var data = process_data(data); console.log(data); }; What ...

Consider: var success = function(data){ data = process_data(data); console.log(data); }; vs. var success = function(data){ var data = process_data(data); console.log(data); }; What ...

I'm using Express with NodeJS and AngularJS in front-end. After click here, add data there, update some record here, show a list there, I get net::ERR_CONNECTION_RESET. It's strange, after 8 or 9 ...

I'm using Express with NodeJS and AngularJS in front-end. After click here, add data there, update some record here, show a list there, I get net::ERR_CONNECTION_RESET. It's strange, after 8 or 9 ...

I have added [?] in right side of the form to show information about respective fields. I want to show information in small box after clicking [?]. How can i do that? Thanks in advance.

I have added [?] in right side of the form to show information about respective fields. I want to show information in small box after clicking [?]. How can i do that? Thanks in advance.

How come I can replace one escaped newline \n from a text area with anything... str1 = str1.replace(/\n /g,"7 "); but it does not work with two of them? str1 = str1.replace(/\n\n /g,"7 "); ...

How come I can replace one escaped newline \n from a text area with anything... str1 = str1.replace(/\n /g,"7 "); but it does not work with two of them? str1 = str1.replace(/\n\n /g,"7 "); ...

  1. replace escaped newline