I have a bunch of paragraphs within a div and one of them has a read_more link. All the paragraphs that come after the read_more link within the same div are hidden by default.
When the read_more is clicked, it displays all these paragraphs that come after the read_more and adds a read_less link at the end of the last paragraph. When the read_less is clicked, it hides all the paragraphs until read_more, displays the read_more link again and hides itself.
I am almost there, please see it here: http://jsfiddle.net/g8tevwb1/
$('p').find('a.read_more').parent().nextAll().hide();
$('a.read_more').click(function(){
$(this).parent().nextAll().show();
$(this).parent().nextAll('p').last().append('<a href="#" class="read_less">read less</a>');
$(this).hide();
return false;
});
$('a.read_less').click(function(){
$(this).parent().prevUntil('a.read_more').hide();
$(this).hide();
return false;
});
I just dont manage to get the read_less working. Am I doing something wrong appending the read_less link in such way? I can manipulate it, change colors, etc but the click function doesnt work.
Its important that all this is happening within the div in which they are contained, as I have other paragraphs within other divs with other read_more buttons and I dont want them to be affected.
Any help is appreciated!