{"id":88,"date":"2019-11-29T16:26:05","date_gmt":"2019-11-29T16:26:05","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=88"},"modified":"2026-10-10T21:01:54","modified_gmt":"2026-10-10T21:01:54","slug":"toggle-text-inside-div","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/toggle-text-inside-div\/","title":{"rendered":"Toggle Text on Click in WordPress Without jQuery"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes a WordPress page needs text that changes when it is clicked: <strong>Show more<\/strong> that becomes <strong>Show less<\/strong>, a teaser that reveals an answer, or a playful greeting. This snippet toggles text on click: the first click shows the second text, the next click brings back the original, and so on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is plain JavaScript, so it works with block themes, which do not load jQuery on the front end. It also works with the keyboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">(function () {\n\t\/\/ ===== Config =====\n\tvar SELECTOR = '.dpc-toggle-text'; \/\/ Elements that swap their text on click.\n\tvar OTHER_TEXT = 'Bye!'; \/\/ Used when the element has no data-dpc-toggle-text attribute.\n\t\/\/ ===== End of config =====\n\n\tfunction prepare(el) {\n\t\tif (el.hasAttribute('data-dpc-original')) {\n\t\t\treturn;\n\t\t}\n\t\tel.setAttribute('data-dpc-original', el.textContent);\n\t\t\/\/ Make paragraphs, headings and divs usable with the keyboard too.\n\t\tif (el.tagName !== 'BUTTON') {\n\t\t\tel.setAttribute('role', 'button');\n\t\t\tif (!el.hasAttribute('tabindex')) {\n\t\t\t\tel.tabIndex = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction toggle(el) {\n\t\tprepare(el);\n\t\tvar swapped = el.getAttribute('data-dpc-swapped') === '1';\n\t\tel.textContent = swapped ? el.getAttribute('data-dpc-original') : (el.getAttribute('data-dpc-toggle-text') || OTHER_TEXT);\n\t\tel.setAttribute('data-dpc-swapped', swapped ? '0' : '1');\n\t}\n\n\tdocument.querySelectorAll(SELECTOR).forEach(prepare);\n\n\tdocument.addEventListener('click', function (event) {\n\t\tvar el = event.target.closest(SELECTOR);\n\t\tif (el) {\n\t\t\ttoggle(el);\n\t\t}\n\t});\n\n\tdocument.addEventListener('keydown', function (event) {\n\t\tif (event.key !== 'Enter' &amp;&amp; event.key !== ' ') {\n\t\t\treturn;\n\t\t}\n\t\tvar el = event.target.closest ? event.target.closest(SELECTOR) : null;\n\t\tif (el &amp;&amp; el === event.target &amp;&amp; el.tagName !== 'BUTTON') {\n\t\t\tevent.preventDefault();\n\t\t\ttoggle(el);\n\t\t}\n\t});\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the class <code>dpc-toggle-text<\/code> to a Paragraph or Heading block (<strong>Advanced \u2192 Additional CSS class(es)<\/strong>). Its own text is the first text, and the second one is <code>OTHER_TEXT<\/code> in the script. Each element can have its own second text in a Custom HTML block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\">&lt;button type=\"button\" class=\"dpc-toggle-text\" data-dpc-toggle-text=\"Bye!\"&gt;Hello World&lt;\/button&gt;<\/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 narrow it to the pages that need the script. Under <strong>Script location<\/strong> tick <strong>Footer<\/strong>, set <strong>Footer Mode<\/strong> to <strong>Javascript<\/strong>, paste the code into the footer editor and 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>, and it adds the <code>&lt;script&gt;<\/code> tag itself, so paste the code without one.<\/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>One click listener on the document handles every <code>.dpc-toggle-text<\/code> element, including elements added to the page later.<\/li>\n\n\n<li>The first time, the script remembers the original text in <code>data-dpc-original<\/code>. Each click swaps between that text and <code>data-dpc-toggle-text<\/code> (or <code>OTHER_TEXT<\/code>).<\/li>\n\n\n<li>Elements that are not a <code>&lt;button&gt;<\/code> get <code>role=\"button\"<\/code> and <code>tabindex=\"0\"<\/code>, and <strong>Enter<\/strong> or <strong>Space<\/strong> toggles them, so keyboard and screen reader users can use them too. A real <code>&lt;button&gt;<\/code> handles that itself.<\/li>\n\n\n<li>The older version was a jQuery plugin with the texts hard-coded in the call.<\/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>Other texts: change <code>OTHER_TEXT<\/code>, or set <code>data-dpc-toggle-text<\/code> per element.<\/li>\n\n\n<li>Toggle content as well: in <code>toggle()<\/code>, add <code>document.getElementById(el.getAttribute('aria-controls')).hidden = !swapped;<\/code> and give the element <code>aria-controls<\/code> with the ID of the content. For a ready-made version see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/toggle-between-two-elements\/\">Toggle between two elements<\/a>.<\/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>The script replaces the text, so bold, links or icons inside the element are lost after the first click. Put the class on the element that contains only the text.<\/li>\n\n\n<li>Do not put the class on a Buttons block wrapper or a link: replacing its text would remove the link inside.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Swap an element&#8217;s text between two values on each click in WordPress, keyboard accessible and without jQuery.<\/p>\n","protected":false},"author":1,"featured_media":46894,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Toggle Text on Click in WordPress Without jQuery","description":"Swap an element's text between two values on each click in WordPress, keyboard accessible and without jQuery."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-88","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\/88","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=88"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":46893,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/88\/revisions\/46893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46894"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=88"}],"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=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=88"},{"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=88"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}