{"id":45826,"date":"2022-07-22T17:19:20","date_gmt":"2022-07-22T17:19:20","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45826"},"modified":"2026-10-10T20:57:57","modified_gmt":"2026-10-10T20:57:57","slug":"toggle-between-two-elements","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/toggle-between-two-elements\/","title":{"rendered":"Toggle Between Two Elements in WordPress With a Button"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes a WordPress page needs to toggle between two elements: monthly and yearly prices, a short and a long version of a text, metric and imperial units. One element is visible, a click shows the other one instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post toggled a CSS class with jQuery on a click anywhere in a <code>&lt;div&gt;<\/code>, and left the CSS that hides the inactive element to you. This version is complete: a real button, two panels, the HTML <code>hidden<\/code> attribute to hide one of them, and a few lines of plain JavaScript. It works without any CSS and without jQuery.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Toggle between two elements: a button switches which of the two panels is shown.\n * Scripts Organizer: Script location \"Shortcode\", output via &#91;scorg_shortcode id=\"\u2026\"&#93;.\n * Edit the labels and the two panels below.\n *\/\n$dpc_toggle = array(\n\t'show_second' =&gt; __( 'Show yearly prices', 'dpc' ),\n\t'show_first'  =&gt; __( 'Show monthly prices', 'dpc' ),\n\t'first'       =&gt; '&lt;p&gt;&lt;strong&gt;Monthly:&lt;\/strong&gt; $19 per month, cancel anytime.&lt;\/p&gt;',\n\t'second'      =&gt; '&lt;p&gt;&lt;strong&gt;Yearly:&lt;\/strong&gt; $190 per year, two months free.&lt;\/p&gt;',\n);\n\n\/\/ Unique IDs, so the shortcode can be used more than once on a page.\n$dpc_toggle_id = wp_unique_id( 'dpc-toggle-' );\n?&gt;\n&lt;div class=\"dpc-toggle\" data-dpc-toggle&gt;\n\t&lt;button type=\"button\" class=\"dpc-toggle__button\" aria-controls=\"&lt;?php echo esc_attr( $dpc_toggle_id . '-1 ' . $dpc_toggle_id . '-2' ); ?&gt;\" data-dpc-label=\"&lt;?php echo esc_attr( $dpc_toggle&#91;'show_first'&#93; ); ?&gt;\"&gt;&lt;?php echo esc_html( $dpc_toggle&#91;'show_second'&#93; ); ?&gt;&lt;\/button&gt;\n\t&lt;div class=\"dpc-toggle__panel\" id=\"&lt;?php echo esc_attr( $dpc_toggle_id . '-1' ); ?&gt;\" aria-live=\"polite\"&gt;&lt;?php echo wp_kses_post( $dpc_toggle&#91;'first'&#93; ); ?&gt;&lt;\/div&gt;\n\t&lt;div class=\"dpc-toggle__panel\" id=\"&lt;?php echo esc_attr( $dpc_toggle_id . '-2' ); ?&gt;\" aria-live=\"polite\" hidden&gt;&lt;?php echo wp_kses_post( $dpc_toggle&#91;'second'&#93; ); ?&gt;&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;?php\n\/\/ The script once per page, in the footer, however many toggles the page has.\nif ( ! wp_script_is( 'dpc-toggle', 'enqueued' ) ) {\n\t$dpc_toggle_js = &lt;&lt;&lt;'JS'\ndocument.addEventListener('click', function (event) {\n\tvar button = event.target.closest('&#91;data-dpc-toggle&#93; &gt; .dpc-toggle__button');\n\tif (!button) {\n\t\treturn;\n\t}\n\tbutton.parentElement.querySelectorAll(':scope &gt; .dpc-toggle__panel').forEach(function (panel) {\n\t\tpanel.hidden = !panel.hidden;\n\t});\n\t\/\/ Swap the visible label with the one stored in data-dpc-label.\n\tvar next = button.getAttribute('data-dpc-label');\n\tbutton.setAttribute('data-dpc-label', button.textContent);\n\tbutton.textContent = next;\n});\nJS;\n\twp_register_script( 'dpc-toggle', false, array(), null, array( 'in_footer' =&gt; true ) );\n\twp_add_inline_script( 'dpc-toggle', $dpc_toggle_js );\n\twp_enqueue_script( 'dpc-toggle' );\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\">This snippet uses Scripts Organizer&#8217;s own <strong>Shortcode<\/strong> script location, so the markup lives in the code block and you edit it there, not in the page.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n<li>Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong> and leave <strong>Template<\/strong> on <strong>All<\/strong>. The Shortcode location is only offered with the Conditions trigger.<\/li>\n\n\n<li>Under <strong>Script location<\/strong> tick <strong>Shortcode<\/strong>, paste the code into the PHP editor, edit the labels and panels in <code>$dpc_toggle<\/code>, and publish.<\/li>\n\n\n<li>Scripts Organizer now shows a shortcode such as <code>&#91;scorg_shortcode id=\"123\"&#93;<\/code> for this block. Copy it, add a <strong>Shortcode<\/strong> block where the toggle should appear, and paste it there. Page builders take it in their shortcode element.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The Shortcode location fits here because the toggle has no settings: its content is the HTML you write in the block. Scripts Organizer includes the file every time the shortcode renders, so the code declares no functions and the script is added only once per page.<\/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>The button is a real <code>&lt;button&gt;<\/code>, so it works with the keyboard and screen readers. <code>aria-controls<\/code> points to both panels, and <code>aria-live<\/code> announces the panel that appears.<\/li>\n\n\n<li>The second panel starts with the <code>hidden<\/code> attribute. A click flips <code>hidden<\/code> on both panels and swaps the button text with the one stored in <code>data-dpc-label<\/code>.<\/li>\n\n\n<li><code>wp_unique_id()<\/code> gives each toggle its own IDs, so the same shortcode can be on a page several times, and one delegated click listener handles all of them.<\/li>\n\n\n<li>The labels are escaped, and the panels go through <code>wp_kses_post()<\/code>, so they accept the same HTML as a post.<\/li>\n\n\n<li>The script is registered as a handle without a file and printed once in the footer, only on pages that show a toggle.<\/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>Different content: change <code>first<\/code>, <code>second<\/code> and the two labels in <code>$dpc_toggle<\/code>. For a second toggle with other content, duplicate the code block.<\/li>\n\n\n<li>Style it with the classes <code>dpc-toggle<\/code>, <code>dpc-toggle__button<\/code> and <code>dpc-toggle__panel<\/code>, for example with a Header CSS part in the same block or in your theme.<\/li>\n\n\n<li>Need the same toggle in several places with different content? Turn the markup into a normal <code>add_shortcode()<\/code> with attributes in an <strong>Everywhere<\/strong> PHP block.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visitors without JavaScript see only the first panel.<\/li>\n\n\n<li>A theme rule like <code>display: block<\/code> on the panels can override <code>hidden<\/code>. Add <code>.dpc-toggle__panel&#91;hidden&#93;{display:none}<\/code> if the second panel shows from the start.<\/li>\n\n\n<li>Conditions narrower than <strong>All<\/strong> also decide where the shortcode renders: on other pages it outputs nothing.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Toggle between two elements in WordPress: one button switches between two content panels, for example monthly and yearly prices. No jQuery.<\/p>\n","protected":false},"author":1,"featured_media":46866,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Toggle Between Two Elements in WordPress With a Button","description":"Toggle between two elements in WordPress: one button switches between two content panels, for example monthly and yearly prices. No jQuery."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-45826","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\/45826","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=45826"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45826\/revisions"}],"predecessor-version":[{"id":46865,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45826\/revisions\/46865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46866"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45826"}],"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=45826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45826"},{"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=45826"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}