I have function that shows full content of div.post-text when I click on div.post-read and when click again then hide it.
But something doesn’t work right, when I do that with first text everything works great, but if I click on second or third when first is open, then it doesn’t slide back or sometimes doesn’t open.
What is wrong with my function?
$(document).ready(function() {
var read = $(".post-read");
read.click(function() {
if (read.html() == 'Read more') {
$(this).html("Show less");
$(this).prev().removeClass('post-text').addClass('post-text-open');
$(this).prev().hide();
$(this).prev().slideToggle("slow");
} else {
$(this).html("Read more");
$(this).prev().removeClass('post-text-open').addClass('post-text');
$(this).prev().hide();
$(this).prev().slideToggle("slow");
}
});
});
HTML:
<div id="posts">
<div class="post">
<div class="post-img">
<img alt="" src="img/post-pic.png">
</div>
<div class="post-text">text ... ... ...</div>
<div class="post-read">Read more</div>
<div class="clear"></div>
</div>
<div class="post">
<div class="post-img">
<img alt="" src="img/post-pic.png">
</div>
<div class="post-text">text ... ... ...</div>
<div class="post-read">Read more</div>
<div class="clear"></div>
</div>
<div class="post">
<div class="post-img">
<img alt="" src="img/post-pic.png">
</div>
<div class="post-text">text ... ... ...</div>
<div class="post-read">Read more</div>
<div class="clear"></div>
</div>
</div>
CSS:
.post-text {float: left;width: 457px;padding-left: 16px; overflow:hidden; height:72px;}
.post-text-open {float: left;width: 457px;padding-left: 16px; overflow: auto; height:auto;}
Change this
to this
See DEMO.