I want to cut text based on a fixed height. When the text is disabled, the "more" link is used to expand the text. When the text expands, a less link is used to smooth the text. I wrote js like this:
$(document).ready(function () { // line height in 'px' var maxheight=218; var showText = "More"; var hideText = "Less"; $('.textContainer_Truncate').each(function () { var text = $(this); if (text.height() > maxheight){ text.css({ 'overflow': 'hidden','height': maxheight + 'px' }); var link = $('<a href="#">' + showText + '</a>'); var linkDiv = $('<div></div>'); linkDiv.append(link); $(this).after(linkDiv); link.click(function (event) { event.preventDefault(); if (text.css('height') == 'auto') { $(this).html(showText); text.css('height', maxheight + 'px'); } else { $(this).html(hideText); text.css('height', 'auto'); } }); } }); });
Html code:
<div class="textContainer_Truncate"> <p>content</p> </div>
My problem is that the more link works, but the less link does not. That is, when more is pressed, the text expands, but it does not return when less is pressed. What is wrong here? Thanks
source share