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

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.


Solution 1:

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/

Solution 2:

$('#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.

Solution 3:

I know this question is a little old (and has had it's answer selected already) but for those wanting another option that're coming across this question through Google (like I did), I found this dynamic text shortener:

Dynamically shortened Text with “Show More” link using jQuery

I found it was better because you could set the character limit, rather than extra spans in the code, or setting a specific height to a container.
Hope it helps someone else out!

Solution 4:

Show More, Show Less (Only when needed) No JQuery

I needed this functionality for an RSS feed on our company's website. This is what I came up with:

// Create Variables
var allOSB = [];
var mxh = '';

window.onload = function() {
  // Set Variables
  allOSB = document.getElementsByClassName("only-so-big");
  
  if (allOSB.length > 0) {
    mxh = window.getComputedStyle(allOSB[0]).getPropertyValue('max-height');
    mxh = parseInt(mxh.replace('px', ''));
    
    // Add read-more button to each OSB section
    for (var i = 0; i < allOSB.length; i++) {
      var el = document.createElement("button");
      el.innerHTML = "Read More";
      el.setAttribute("type", "button");
      el.setAttribute("class", "read-more hid");
      
      insertAfter(allOSB[i], el);
    }
  }

  // Add click function to buttons
  var readMoreButtons = document.getElementsByClassName("read-more");
  for (var i = 0; i < readMoreButtons.length; i++) {
    readMoreButtons[i].addEventListener("click", function() { 
      revealThis(this);
    }, false);
  }
  
  // Update buttons so only the needed ones show
  updateReadMore();
}
// Update on resize
window.onresize = function() {
  updateReadMore();
}

// show only the necessary read-more buttons
function updateReadMore() {
  if (allOSB.length > 0) {
    for (var i = 0; i < allOSB.length; i++) {
      if (allOSB[i].scrollHeight > mxh) {
        if (allOSB[i].hasAttribute("style")) {
          updateHeight(allOSB[i]);
        }
        allOSB[i].nextElementSibling.className = "read-more";
      } else {
        allOSB[i].nextElementSibling.className = "read-more hid";
      }
    }
  }
}

function revealThis(current) {
  var el = current.previousElementSibling;
  if (el.hasAttribute("style")) {
    current.innerHTML = "Read More";
    el.removeAttribute("style");
  } else {
    updateHeight(el);
    current.innerHTML = "Show Less";
  }
}

function updateHeight(el) {
  el.style.maxHeight = el.scrollHeight + "px";
}

// thanks to karim79 for this function
// http://stackoverflow.com/a/4793630/5667951
function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
@import url('https://fonts.googleapis.com/css?family=Open+Sans:600');

*,
*:before,
*:after {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
body {
  font-family: 'Open Sans', sans-serif;
}
h1 {
  text-align: center;
}
.main-container {
  margin: 30px auto;
  max-width: 1000px;
  padding: 20px;
}
.only-so-big p {
  padding: 0;
  margin: 0;
}
p {
  font-size: 15px;
  line-height: 20px;
}
hr {
  background: #ccc;
  display: block;
  height: 1px;
  width: 100%;
}


/*
  NECESSARY SECTION
*/
.only-so-big {
  background: rgba(178, 252, 255, .3);
  height: 100%;
  max-height: 100px;
  overflow: hidden;
  -webkit-transition: max-height .75s;
  transition: max-height .75s;
}

.read-more {
  background: none;
  border: none;
  color: #1199f9;
  cursor: pointer;
  font-size: 1em;
  outline: none; 
}
.read-more:hover {
  text-decoration: underline;
}
.read-more:focus {
  outline: none;
}
.read-more::-moz-focus-inner {
  border: 0;
}
.hid {
  display: none;
}
<div class="main-container">
      <h1>Controlling Different Content Size</h1>
      <p>We're working with an RSS feed and have had issues with some being much larger than others (content wise). I only want to show the "Read More" button if it's needed.</p>
      <div class="only-so-big">
        <p>This is just a short guy. No need for the read more button.</p>
      </div>
      <hr>
      <div class="only-so-big">
        <p>This one has way too much content to show. Best be saving it for those who want to read everything in here.</p>
        <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
          voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi THE END!</p>
      </div>
      <hr>
      <div class="only-so-big">
        <p>Another small section with not a lot of content</p>
      </div>
      <hr>
      <div class="only-so-big">
        <p>Make Window smaller to show "Read More" button.<br> totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed?</p>
      </div>
    </div>