Solved

show hide text (first 90 words) with read more link

Posted on 2009-07-07
11
495 Views
Last Modified: 2012-05-07
Trying to find the easiest solution to implementing a read more link that would read the first 90 or so words from a string and truncate it by appending a "...more" link. upon clicking the link the remainder of the text would be revealed. I know that it is done with hide and show divs but how can I get it to only show example 90 words or a certain amount of characters?

your help is appreciated.  
thanks in advance.
0
Comment
Question by:jriver12
  • 6
  • 5
11 Comments
 
LVL 17

Expert Comment

by:selvol
ID: 24800464
0
 
LVL 17

Expert Comment

by:selvol
ID: 24802741
Yes that on is set at 100 chars.
Here's 90 word (90 words @ 3 letters) 270 . But you knew that.
 

<!--  http://www.barelyfitz.com/projects/truncate/ --><html>
<head>
<p id="truncateMe">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>
 
<script type="text/javascript">
 
var len = 270;
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;
  }
}
 
</script>
 
 
</body>
</html>

Open in new window

0
 

Author Comment

by:jriver12
ID: 24802950
selvol:
Awesome!  one quick question.  is there a way to minimize it again?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:jriver12
ID: 24803132
selvol:
Did a bit more testing:  the script works great for one <p> using the id=truccateme, however we have multiple <p>  that we would like to truncate.  we are trying to use this fuctionality in our FAQ  whereby when we show the question and only a certain amount of the answer  until the click the ....  any suggestions?
0
 
LVL 17

Expert Comment

by:selvol
ID: 24803305
Sure 2 mins,
 
 
0
 
LVL 17

Accepted Solution

by:
selvol earned 500 total points
ID: 24803394
You can always fall back on on toggle..
 
I'll check out the multi <p>'s on a page.
a few mins.
 
Selvol

<script type="text/javascript">
function toggleContent() {
  var contentId = document.getElementById("content");
  contentId.style.display == "block" ? contentId.style.display = "none" :
contentId.style.display = "block";
}
</script>
   <td class="info">BROWN</td>
   </tr>
    </table>
     </td>
      </tr>
       </table>
        </tr>
         </table>      <tr>
            <table width="433">
             <tr class="charge_info">
      <td><div ID="content" style="display:block;"><p id="t">
               In Custody For:<br>
	MISDEMEANOR  ASSAULT INTENT RECKLESS INJURE<br>
	MISDEMEANOR  DISORDERLY CONDUCT-FIGHTING   <br>
     </td></p>
      </tr>
       </table>
 
 
<button style="background-color: #ffffff;font: 6;border: 0;" onclick="toggleContent()">Toggle</button>
 
 
 
  </table><br>
<h1 id="newTitle">JON, RUDY DOE</h1>#P543238</td>
	<table  width="400" border="0">
<tr class="inmate_info">
	<td colspan="2">Date: May -14-2009</td>
 
<script type="text/javascript"> 
var len = 21;
var p = document.getElementById('t');
if (p) { 
  var trunc = p.innerHTML;
  if (trunc.length > len) { 
    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;">' +
      'Click to see<\/a>';
    p.innerHTML = trunc;
  }
} 
</script>

Open in new window

0
 
LVL 17

Expert Comment

by:selvol
ID: 24803603
Apologies, I have to return some time later? have you looked at http://jqueryui.com/ before?
 
Super stuff......
Selvol
0
 

Author Comment

by:jriver12
ID: 24803727
yes.  I will relook though.
0
 
LVL 17

Expert Comment

by:selvol
ID: 24876989

Did you ever solve this?.
 
Selvol
0
 

Author Comment

by:jriver12
ID: 24959830
selvol, no I have not. sorry in the delay at getting back but I was out of town.
0
 

Author Closing Comment

by:jriver12
ID: 31600919
thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question