Jake
Jake

Reputation: 31

Why won't this text truncation javascript work?

I found this snippet online which works to truncate text:

http://www.barelyfitz.com/projects/truncate/

The problem is, the script puts the href inside the container where the text to be truncated lies and when clicked the rest of the test expands but the show more link (the dot dot dots) is gone. I need that link to always be present since I want the user to be able to toglle show more/less kind of like how they do in youtube.

How can he script be altered to work this way, or how will a new script for this look. This was my failed attempt to make the aforementioned script work the way I needed it to:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

<p id="truncateMe" style="border: 1px solid red">Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Aenean consectetuer. Etiam venenatis. Sed ultricies, pede sit
amet aliquet lobortis, nisi ante sagittis sapien, in rhoncus lectus
mauris quis massa. Integer porttitor, mi sit amet viverra faucibus,
urna libero viverra nibh, sed dictum nisi mi et diam. Nulla nunc eros,
convallis sed, varius ac, commodo et, magna. Proin vel
risus. Vestibulum eu urna. Maecenas lobortis, pede ac dictum pulvinar,
nibh ante vestibulum tortor, eget fermentum urna ipsum ac neque. Nam
urna nulla, mollis blandit, pretium id, tristique vitae, neque. Etiam
id tellus. Sed pharetra enim non nisl.</p>

<div id="link"></div>

<script type="text/javascript">

var len = 100;
var p = document.getElementById('truncateMe');
if (p) {

  var trunc = p.innerHTML;
  if (trunc.length > len) {

    /* Truncate the content of the P, then go back to the end of the
       previous word to ensure that we don't truncate in the middle of
       a word */
    trunc = trunc.substring(0, len);
    trunc = trunc.replace(/\w+$/, '');

    /* Add an ellipses to the end and make it a link that expands
       the paragraph back to its original size */
    trunc += '<a href="#" ' +
      'onclick="this.parentNode.innerHTML=' +
      'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
      '...<\/a>';
    p.innerHTML = trunc;


    trunc2 = '<a href="#" ' +
      'onclick="$(\'truncateMe\').innerHTML=' +
      'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
      'More<\/a>';


    $('#link').html(trunc2);

  }
}

</script>

Upvotes: 1

Views: 399

Answers (2)

Praveen Lobo
Praveen Lobo

Reputation: 7187

The following doesn't need a DIV. I just rewrote your code to make it work. May be you can enhance it to fully to use jQuery etc...

<script type="text/javascript">

// Set the variables
var len = 100;
// the following will add the less link
var less = "<a href='#' onclick='toggleDisplay(); return false;'>less</a>";
var p = document.getElementById('truncateMe');

// This method handles the more/less stuff
function toggleDisplay(){
if (p) {

  var trunc = p.innerHTML;
  if (trunc.length > len) {
    p.innerHTML = trunc + less;
    less = "";// so that it doesn't keep adding less at the end everytime

    /* Truncate the content of the P, then go back to the end of the
       previous word to ensure that we don't truncate in the middle of
       a word */
    trunc = trunc.substring(0, len);
    trunc = trunc.replace(/\w+$/, '');

    /* Add an ellipses to the end and make it a link that expands
       the paragraph back to its original size */
    trunc += '<a href="#" ' +
      'onclick="this.parentNode.innerHTML=' +
      'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
      'more<\/a>';
    p.innerHTML = trunc;
  }
}//end if
}//end method

// Calling outside ( call it on page load...)
toggleDisplay();

</script>

Upvotes: 0

RobG
RobG

Reputation: 147413

One way to do this is to set the div to a fixed size with scroll set to auto. If the content causes scroll bars to appear, words are removed from the end of the content until the scroll bars go away. You can do that by appending a span with display:none to the end of the text, then moving sets of trailing characters starting with a space into the span until you get the desired result.

Then add an ellipsis (which is &hellip; and not just 3 dots) after the span that sits in the bottom right corner. To show/hide the extra content, toggle the display of the span to none or '' (empty string) and expand the div accordingly.

Upvotes: 1

Related Questions