$("div.learnmore").each(function (i, l) { pre = '<p class="label">Learn More</p><div class="learnmore inner">'; post = '</div></p>'; l = $(l); if (l.children("p.label").length === 0) { l.html(pre.concat(l.html(), post)); } }); $("div.learnmore > p.label").click(function () { $(this).siblings(".inner").toggle(); });
<div class="learnmore">This is the explanatory text. There probably should be a lot more text here, but I'll keep it simple for the moment.</div> <div> <p>This is just a random paragraph to take up space</p> </div> <div class="learnmore">This is a second set of explanatory text that has its own Learn More text to click to hide/show</div>