snipt

Ctrl+h for KB shortcuts

JavaScript

Foldable text with headings (jQuery)

1
2
3
4
5
6
var $posttext = $('.post-text');
$posttext.find('h1').nextUntil('h1').hide();
$posttext.find('h1:first').nextUntil('h1').show();
$posttext.find('h1').on('click', function() { 
	$(this).nextUntil('h1').slideToggle(400);
});
https://snipt.net/embed/a0b9c109d617a9a49120c5744c25e1a6/
/raw/a0b9c109d617a9a49120c5744c25e1a6/
a0b9c109d617a9a49120c5744c25e1a6
js
JavaScript
6
2016-12-21T03:41:24
True
False
True
Aug 06, 2013 at 08:08 PM
/api/public/snipt/72403/
foldable-text-with-headings-jquery
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">$posttext</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.post-text&#39;</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$posttext</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">).</span><span class="nx">nextUntil</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">).</span><span class="nx">hide</span><span class="p">();</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">$posttext</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;h1:first&#39;</span><span class="p">).</span><span class="nx">nextUntil</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">).</span><span class="nx">show</span><span class="p">();</span> </span><span id="L-4"><a name="L-4"></a><span class="nx">$posttext</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;click&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">nextUntil</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">).</span><span class="nx">slideToggle</span><span class="p">(</span><span class="mi">400</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
javascript, jquery