When clicking a button I would like the text to toggle and say “read more” or “read less”. If I click an other button, I would like the previous button go back to its original text “read more”
HTML:
<div class="accordion">
<div class="abtn">
<input id="A-1" type="radio" value="A">
<label class="chars" for="A-1">A</label>
<div class="char-more">Learn More</div>
</div>
<div class="acontent">Hello I am content A</div>
<div class="abtn">
<input id="B-1" type="radio" value="B">
<label class="chars" for="B-1">B</label>
<div class="char-more">Learn More</div>
</div>
<div class="acontent">Hello I am content B</div>
<div class="abtn">
<input id="C-1" type="radio" value="C">
<label class="chars" for="C-1">C</label>
<div class="char-more">Learn More</div>
</div>
<div class="acontent">Hello I am content B</div>
</div>
jQuery:
$(".abtn .char-more").click(function(){
$(this).closest('.abtn').nextAll(".acontent").first().slideToggle("fast")
.siblings(".acontent:visible").slideUp('fast');
$(this).html("Read Less");
$(this).siblings(".char-more").html("Read More");
});
Using following html structure and adding an active class to each module you can simplify how you find the previous module activated( if there is one) and also add a little style using css. It’s also a little more intuitive to read than trying to debug siblings that don’t exist and
nestaAll‘sYour code would follow a pattern like: