I am trying to create a script that will expand and collapse a div
containing a paragraph when the header text above it is clicked.
<div class="container">
<h3><span class="toggler">Click here to toggle text</span></h3>
<div class="wrapper">
<p>Random content</p>
</div>
</div>
<div class="container">
<h3><span class="toggler2">Click here to toggle text</span></h3>
<div class="wrapper2">
<p>Random text</p>
</div>
</div>
I know I can use a function that toggles between display: block
and display: none
.
If I have multiple divs with different classes, repeating the same function for each one seems inefficient. There must be a more elegant solution.
$(document).ready(function() {
$(".toggler").on("click", function() {
$(".wrapper").toggleClass("active");
});
});