Solved

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

Posted on 2009-07-07
11
488 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now