0

I want to have a text that overflows in another div, so I found and used this answer from another question at Stackoverflow.

The problem is that only plain text is displayed; links, bold/italics and paragraphs are ignored.

Here is the same jsfiddle from the answer, but with added html tags. How do i get to display them?

Code:

var currentCol = $('.col:first');
var text = currentCol.text();
currentCol.text('');
var wordArray=text.split(' ');

$.fn.hasOverflow = function() {
   var div= document.getElementById( $(this).attr('id') ); 
   return div.scrollHeight>div.clientHeight;
};

for ( var x = 0; x < wordArray.length; x++ ) {
    var word = wordArray[x];
    currentCol.append(word+' ');
    if ( currentCol.hasOverflow() ) {
        currentCol = currentCol.next('.col');
    }
}

Any tips or advice will be appreciated :)

Community
  • 1
  • 1

1 Answers1

0

jQuery .text method returns only plain text. Try using .html instead.
Example:

var text = currentCol.html();

But if your tags contain any spaces (like <span class="some-class">) then the following line from your code will do a mess with your text

var wordArray=text.split(' ');

You might want to change it to

text = text.replace(/ (?![^<>]*>)/gi, '%^%');
var wordArray = text.split('%^%');

This is kind of workaround since you could iterate over each regex match and substring it on every space character but IMHO the above idea with %^% combination is much more simple. you can replace those 3 signs with anything you want. I just thought it is unique enough that won't be used in any other purpose.
Above regular expression is simplified and assumes that you don't use < and > signs in your text.
If you actually do I would recommend to replace them with &lt; and &gt; anyway.

matewka
  • 9,912
  • 2
  • 32
  • 43
  • 1
    As a side-note for the OP, when using `.html()` and the `.split` you will have to factor in that your HTML Tags will also be split. And the result may not be as intended. – Nunners Oct 10 '13 at 08:44
  • Thank you. The `currentCol.html();` works, but `var wordArray=text.replace(/ (?![^<>]*>)/gi, ' ');` displays plain text _with_ html tags, and with a space after each character. – Ihaveagreenavatar Oct 10 '13 at 09:18
  • Right, I got confused and forgot that you want to split it, not replace spaces. :) Editing the answer... – matewka Oct 10 '13 at 09:42
  • I fixed the regex. Take a look please. – matewka Oct 10 '13 at 10:02
  • That works much better, but i get an extra `

    ` after the first word of a new paragraph. In example the text is displaying like this: `

    Lorem

    ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.

    `
    – Ihaveagreenavatar Oct 10 '13 at 11:29
  • Maybe worth mentioning: [here](http://ruudvoesten.nl/index6.php) is the site i'm building (with a CMS: Couch CMS), and it all about the _biografie_ section. Maybe the `

    ` error is couch related...

    – Ihaveagreenavatar Oct 10 '13 at 11:34
  • It must be related to the CMS. In my regex there is no way that it could add any HTML tags. – matewka Oct 10 '13 at 12:10
  • Ok i'll check that. Thanks for your help :) – Ihaveagreenavatar Oct 10 '13 at 12:42
  • Well, if my answer resolved your main problem it would be nice if you accepted it. – matewka Oct 10 '13 at 12:44
  • Done. Sorry, i'm new overhere. – Ihaveagreenavatar Oct 10 '13 at 12:53
  • Hey matewka, sorry i'm getting back to you, but i [posted](http://www.couchcms.com/forum/viewtopic.php?f=4&t=7705) the problem on the couch cms forum, and someone mentioned it's the javascript that is causing the extra `

    `'s. If i disable the whole currentCol section the problem is gone. Could you please take a look again?

    – Ihaveagreenavatar Oct 10 '13 at 19:09