{"id":81,"date":"2019-11-29T16:06:42","date_gmt":"2019-11-29T16:06:42","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=81"},"modified":"2026-10-10T21:01:38","modified_gmt":"2026-10-10T21:01:38","slug":"slide-toggle-with-jquery","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/slide-toggle-with-jquery\/","title":{"rendered":"Slide Toggle Content on Click in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A slide toggle in WordPress shows or hides a block of content when a visitor clicks a button: an FAQ answer, a \u201cRead more\u201d section, a filter panel. The classic way is jQuery&#8217;s <code>slideToggle()<\/code>, but block themes no longer load jQuery on the front end, so the old snippet stops with \u201cjQuery is not defined\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This version does the same slide in plain JavaScript with the Web Animations API. It is keyboard accessible, tells screen readers whether the content is open, and respects the reduced motion setting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The markup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Give the clickable element the class <code>trigger<\/code> and the content the class <code>toggled-object<\/code>. In the block editor, use <strong>Advanced \u2192 Additional CSS class(es)<\/strong> on a Heading or Paragraph block for the trigger and on a Group block for the content; the script makes the trigger keyboard accessible. In hand-written HTML, use a <code>&lt;button&gt;<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\">&lt;button type=\"button\" class=\"trigger\"&gt;Show details&lt;\/button&gt;\n\n&lt;div class=\"toggled-object\"&gt;\n\t&lt;p&gt;Hidden content that slides open.&lt;\/p&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Several toggles on one page: give each content block an <strong>HTML anchor<\/strong> (for example <code>faq-1<\/code>) and add <code>data-target=\"#faq-1\"<\/code> to its trigger. Without <code>data-target<\/code>, a trigger toggles every <code>.toggled-object<\/code>, as the old jQuery version did.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The CSS<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* The toggled content starts closed. *\/\n.toggled-object {\n\tdisplay: none;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The JavaScript<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">(function () {\n\tvar DURATION = 300; \/\/ Animation length in milliseconds.\n\tvar reduceMotion = window.matchMedia &amp;&amp; window.matchMedia('(prefers-reduced-motion: reduce)').matches;\n\n\tfunction targetsFor(trigger) {\n\t\t\/\/ data-target=\"#faq-1\" toggles one element; without it, every .toggled-object.\n\t\treturn document.querySelectorAll(trigger.getAttribute('data-target') || '.toggled-object');\n\t}\n\n\tfunction isOpen(el) {\n\t\tif (el.dpcOpen === undefined) {\n\t\t\tel.dpcOpen = !el.hidden &amp;&amp; window.getComputedStyle(el).display !== 'none';\n\t\t}\n\t\treturn el.dpcOpen;\n\t}\n\n\tfunction animate(el, from, to, done) {\n\t\tif (el.dpcSlide) {\n\t\t\tel.dpcSlide.cancel();\n\t\t}\n\t\tif (reduceMotion || !el.animate) {\n\t\t\tif (done) {\n\t\t\t\tdone();\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tel.style.overflow = 'hidden';\n\t\tel.dpcSlide = el.animate(&#91;{ height: from + 'px' }, { height: to + 'px' }&#93;, { duration: DURATION, easing: 'ease' });\n\t\tel.dpcSlide.onfinish = function () {\n\t\t\tel.style.overflow = '';\n\t\t\tel.dpcSlide = null;\n\t\t\tif (done) {\n\t\t\t\tdone();\n\t\t\t}\n\t\t};\n\t}\n\n\tfunction open(el) {\n\t\tvar from = el.dpcSlide ? el.offsetHeight : 0;\n\t\tel.dpcOpen = true;\n\t\tel.hidden = false;\n\t\tel.style.removeProperty('display');\n\t\tif (window.getComputedStyle(el).display === 'none') {\n\t\t\tel.style.display = 'block';\n\t\t}\n\t\tanimate(el, from, el.scrollHeight);\n\t}\n\n\tfunction close(el) {\n\t\tel.dpcOpen = false;\n\t\tanimate(el, el.offsetHeight, 0, function () {\n\t\t\tel.style.display = 'none';\n\t\t});\n\t}\n\n\tfunction toggle(trigger) {\n\t\tvar targets = targetsFor(trigger);\n\t\tvar opened = false;\n\t\ttargets.forEach(function (el) {\n\t\t\tif (isOpen(el)) {\n\t\t\t\tclose(el);\n\t\t\t} else {\n\t\t\t\topen(el);\n\t\t\t\topened = true;\n\t\t\t}\n\t\t});\n\t\ttrigger.setAttribute('aria-expanded', opened ? 'true' : 'false');\n\t}\n\n\tdocument.querySelectorAll('.trigger').forEach(function (trigger) {\n\t\tvar targets = targetsFor(trigger);\n\t\tif (!targets.length) {\n\t\t\treturn;\n\t\t}\n\t\tvar native = trigger.tagName === 'BUTTON';\n\t\tif (!native) {\n\t\t\ttrigger.setAttribute('role', 'button');\n\t\t\ttrigger.tabIndex = 0;\n\t\t}\n\t\ttrigger.setAttribute('aria-expanded', isOpen(targets&#91;0&#93;) ? 'true' : 'false');\n\t\tif (targets&#91;0&#93;.id) {\n\t\t\ttrigger.setAttribute('aria-controls', targets&#91;0&#93;.id);\n\t\t}\n\n\t\ttrigger.addEventListener('click', function (event) {\n\t\t\tevent.preventDefault();\n\t\t\ttoggle(trigger);\n\t\t});\n\t\tif (!native) {\n\t\t\ttrigger.addEventListener('keydown', function (event) {\n\t\t\t\tif (event.key === 'Enter' || event.key === ' ') {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\ttoggle(trigger);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t});\n})();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong> and set <strong>Template<\/strong> to <strong>All<\/strong> (or only the pages that use the toggle). Under <strong>Script location<\/strong> tick <strong>Header<\/strong> and <strong>Footer<\/strong>. Put the CSS in the header with <strong>Header Mode<\/strong> <strong>CSS<\/strong>, and the JavaScript in the footer with <strong>Footer Mode<\/strong> <strong>Javascript<\/strong>, then publish.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scripts Organizer prints header and footer code on the front end only with the <strong>Conditions<\/strong> trigger, not with <strong>Everywhere<\/strong>. The footer runs after the page content, so no document-ready wrapper is needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>On click the script checks whether the content is open. Opening shows it and animates its height from 0 to its full <code>scrollHeight<\/code>; closing animates to 0 and then sets <code>display: none<\/code>.<\/li>\n\n\n<li><code>el.animate()<\/code> runs the animation in the browser without jQuery. Clicking again during a slide cancels it and slides back from the current height.<\/li>\n\n\n<li>The trigger gets <code>aria-expanded<\/code> and, when the content has an id, <code>aria-controls<\/code>. A trigger that is not a <code>&lt;button&gt;<\/code> also gets <code>role=\"button\"<\/code>, focus with Tab and Enter\/Space support.<\/li>\n\n\n<li>With reduced motion turned on, the content opens and closes without animating.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Speed: change <code>DURATION<\/code>.<\/li>\n\n\n<li>Start open: delete the CSS. The first click then closes the content.<\/li>\n\n\n<li>Other class names: change <code>.trigger<\/code> in <code>querySelectorAll()<\/code> and <code>.toggled-object<\/code> in <code>targetsFor()<\/code> and the CSS.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Variant: jQuery<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If your site already loads jQuery and you prefer it, this is the old snippet in a safe wrapper. On a block theme, load jQuery first with <code>wp_enqueue_script( 'jquery' )<\/code>. See <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/how-to-start-with-jquery-in-wordpress\/\">How to start with jQuery in WordPress<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">jQuery(function ($) {\n\t$('.trigger').on('click', function () {\n\t\t$('.toggled-object').slideToggle(300);\n\t});\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Padding on the toggled element itself stays visible during the slide. Put the padding on an inner Group instead.<\/li>\n\n\n<li>Content hidden with CSS is also hidden when JavaScript fails. For content that must always be reachable, consider the Details block, which opens and closes without any script.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Slide content open and closed on click in WordPress with plain JavaScript instead of jQuery slideToggle, keyboard accessible and motion aware.<\/p>\n","protected":false},"author":1,"featured_media":46892,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Slide Toggle Content on Click in WordPress - Code DPlugins","description":"Slide content open and closed on click in WordPress with plain JavaScript instead of jQuery slideToggle, keyboard accessible and motion aware."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-81","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":46891,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/81\/revisions\/46891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46892"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=81"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=81"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}