use jQuery to expand/collapse text
<p>I want to cut off text based on a fixed height.When the text is cut off, a "more" link is used to expand to text. When the text is expanded, a "less" link is used to collapse the text. I wrote the js as this:</p>

<pre><code>$(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() &gt; maxheight){
            text.css({ 'overflow': 'hidden','height': maxheight + 'px' });

            var link = $('&lt;a href="#"&gt;' + showText + '&lt;/a&gt;');
            var linkDiv = $('&lt;div&gt;&lt;/div&gt;');
            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');
              }
            });
        }       
      });
  });
</code></pre>

<p>The html code is:</p>

<pre><code>&lt;div class="textContainer_Truncate"&gt;
  &lt;p&gt;content&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>My problem is that the "more" link works but the "less" doesn't. That is, when more is clicked, the text is expanded but it won't go back when the less is clicked.What's wrong here?
Thanks</p>
