tags:

views:

47

answers:

1

Here is my html:

<a href="#">Read more</a>

<div class="moreDetails">
  <p class="additionalText">Some text help here, random length.</p>
  <p class="author">
    <span class="bolder"><a href="minidashboard.php?user_url=http://url.people/1332517"&gt;Name&lt;/a&gt;
    </span>
  </p>
<div class="replies">
  <span>
    <a href="topic.php?id=http://url/topics/1049198"&gt;1&lt;/a&gt;
  </span>
</div>

Im then using jQuery to add a class to the .additionalText div when its text is longer than 36 chars.

jQuery:

$('.moreDetails p.additionalText').filter(function () {
  if ($(this).text().length > 32) {
    $(this).addClass('trim');
  }
});

What I now want is when <a href="#">Read more</a> is clicked the class .trim to be removed and reveal the content.

.trim sets the paragraph to a fixed height with overflow set to hidden.

+4  A: 

You can give that link a class, like this:

<a class="readMore" href="#">Read more</a>

then add a click handler, like this:

$("a.readMore").click(function() {
  $(this).next().find('.additionalText').removeClass('trim');
});

This works by finding the <div> relative to the <a> via .next() and .find(). If the <div> doesn't immediately follow the <a> like in your posted code this may need some tweaks, for example if there are elements in-between but they're still siblings you'd need .nextAll('.moreDetails:first') instead of .next().

Nick Craver