I have paragraph which has more than 500 character. I want to get only initial 100 character and hide rest of it. Also I want to insert "More" link next to 100 character. On click of more link whole paragraph should display and edit text "More" to "Less" and on click "Less" it should toggle behavior. Paragraph is dynamically generated I cant wrap content of it using .wrap(). Here is example what I have and what I want.
This is what I have :
<p>It is a long established fact that a reader will be distracted by the readable
content of a page when looking at its layout. The point of using Lorem Ipsum is that
it has a more-or-less normal distribution of letters, as opposed to using 'Content
content here', making it look like readable English. Many desktop publishing packages
and web page editors now use Lorem Ipsum as their default model text.</p>
This is what I want when DOM loads
<p>It is a long established fact that a reader will be distracted by ..More</p>
This is what I want when user click "More"
<p>It is a long established fact that a reader will be distracted by the readable
content of a page when looking at its layout. The point of using Lorem Ipsum is that
it has a more-or-less normal distribution of letters, as opposed to using 'Content
content here', making it look like readable English. Many desktop publishing packages
and web page editors now use Lorem Ipsum as their default model text. ..Less</p>
When we click on "Less", it should revert what on click "More" has done.
I am using jQuery to split, slice and wrap substring into span which I want to hide but that doesn't work.
var title = $("p").text();
var shortText = jQuery.trim(title).substring(100, 1000).split(" ")
.slice(0, -1).join(" ") + "...More >>";
shortText.wrap('</span>');
It's not a top google result, but I've used the jQuery Expander plugin to great success. It's nice because it doesn't hide anything from search engine robots.
It looks like a couple other people beat me to it, but here is what I came up with.
for everyone who has come here searching for show more... Here is another plug-in http://viralpatel.net/blogs/dynamically-shortened-text-show-more-link-jquery/
Thanks to @iambriansreed for his nice function, here's a slight modification to truncate paragraph on line breakes:
Fiddle: http://jsfiddle.net/iambriansreed/bjdSF/
jQuery:
Have you looked at the jQuery Truncator plugin?
It pretty much does exactly what you've described.