Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show more text within a certain length (like youtube)

Tags:

jquery

I want to have a text to only be a certain amount of characters/length and after that length, I want to put a link to reveal the full length of the text.

The link will be (more...). And once the user clicks the link (more..) the rest of the text will slide down.

How would I accomplish this?

Heres an example.

blah blah blah blah blah (more...) 

When the user clicks (more..), it will show the entire text.

NOTE: I am taking a about data in a table row/table cell, not just any text.

like image 609
Ben Avatar asked Mar 11 '11 07:03

Ben


People also ask

How do you create show more and show less functionality for hiding text using JavaScript?

The function takes two arguments, a div containing only the words "show more" [or whatever] and a div containing the originally hidden text and the words "show less." The function displays the one div and hides the other. Show activity on this post. You can also use details HTML tag which does the work for you.

How do you make a read more link in HTML?

Go to Site pages (under the Website menu) and begin editing the site page where you want the link to appear. Click the Gadgets icon to display the list of available gadgets. Drag the custom HTML gadget from the Gadget list (not a content gadget), and drop it in the desired location.


2 Answers

The secret about this effect, is wrapping the parts that you want to control with HTML tags.

$(".more").toggle(function(){     $(this).text("less..").siblings(".complete").show();     }, function(){     $(this).text("more..").siblings(".complete").hide();     }); 

<span class="teaser">text goes here</span>  <span class="complete"> this is the  complete text being shown</span>  <span class="more">more...</span> 

Online demo here: http://jsfiddle.net/zA23k/215/

like image 83
amosrivera Avatar answered Sep 21 '22 08:09

amosrivera


$('#more').click(function(e) {     e.stopPropagation();     $('div').css({         'height': 'auto'     }) });  $(document).click(function() {     $('div').css({         'height': '40px'     }) }) 

Check working example at http://jsfiddle.net/7Vv8u/4/

With Animation http://jsfiddle.net/7Vv8u/5/

Click on Read more to expand the text. Click outside to minimize it again.

like image 43
Hussein Avatar answered Sep 18 '22 08:09

Hussein