{"id":91376,"date":"2019-09-05T14:12:08","date_gmt":"2019-09-05T19:12:08","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=91376"},"modified":"2023-06-28T19:13:13","modified_gmt":"2023-06-29T00:13:13","slug":"how-to-manipulate-dom-in-wordpress-with-jquery","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/","title":{"rendered":"Manipulating DOM in WordPress using JQuery"},"content":{"rendered":"\n<p>Thanks to WordPress\u2019 <a href=\"https:\/\/make.wordpress.org\/\">open-source platform<\/a> and vibrant developer community, creating themes for the platform has a pretty wide appeal. Navigating all the elements and content required to build a WordPress site can be challenging, however, depending on your programming knowledge and experience.<\/p>\n\n\n\n<p>Fortunately, the <a href=\"https:\/\/www.w3.org\/DOM\/\">Document Object Model (DOM)<\/a> can make it easier to manipulate or travel through a site\u2019s HTML code. The DOM works by enabling you to create a tree made up of various nodes or objects. This opens up the opportunity to change the display, structure, or function of the site, using JavaScript libraries such as <a href=\"https:\/\/jquery.com\/\">jQuery<\/a>.<\/p>\n\n\n\n<p>In this article, we\u2019ll explain what the DOM is and why you might want to use it. We\u2019ll then provide some standard jQuery manipulation techniques for using the DOM with WordPress. Let\u2019s dive right in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the DOM?<\/h2>\n\n\n\n<p>Simply put, the DOM is an Application Programming Interface (API) for HTML. It creates a tree of objects that represents the content and structure of a web page. It\u2019s also a <a href=\"https:\/\/www.w3.org\/DOM\/\">platform and language-agnostic<\/a> script, which makes it quite versatile. When the DOM represents HTML source code in this way, it makes it accessible so that various programming languages can connect to it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use the DOM?<\/h2>\n\n\n\n<p>As a WordPress user, you are likely seeing the DOM in action any time you encounter dynamic interactivity. This might include when an error is returned if a form is submitted with missing information. Content sliders are another example of the DOM in use:<\/p>\n\n\n\n<p>Developers can use the DOM to update and change nearly all of the elements on a web page. You can use it to build documents and navigate their structure. The DOM can also be used to add, modify, or delete elements and objects.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DOM Manipulation With JQuery<\/h2>\n\n\n\n<p>To be clear, the DOM and HTML are not the same, although they should contain the same elements. The DOM is a modeled representation of HTML source code, and can be altered by <a href=\"https:\/\/wpengine.com\/resources\/wordpress-javascript\/\">client-side JavaScript<\/a>.<\/p>\n\n\n\n<p>Below, we\u2019ll be looking at methods in the jQuery library for testing out some DOM manipulation techniques. Keep in mind that <a href=\"https:\/\/www.html5rocks.com\/en\/tutorials\/internals\/howbrowserswork\/\">different browsers handle the DOM<\/a> in their own way, but you don\u2019t have to do anything special to begin using it. All browsers use some form of the DOM to make pages accessible to JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DOM Manipulation Techniques<\/h2>\n\n\n\n<p>The DOM can be used in a wide variety of ways. To illustrate its flexibility, let\u2019s look at six techniques that can get you started with this useful API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. The Before() Method<\/h3>\n\n\n\n<p>True to its name, the <em>before()<\/em> method is used when you want to insert any element before another target element, as indicated in the code.<\/p>\n\n\n\n<p>In this example, <a href=\"https:\/\/api.jquery.com\/before\/#before-content-content\">the method can be used<\/a> to add a square element with text above a designated element, such as a button, once it is clicked on:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(this).before( \"&lt;div class='square'&gt;Another square&lt;\/div&gt;\" );<\/pre>\n\n\n\n<p>Page placement and layout are considerations to keep in mind when using this method. You\u2019ll want to make sure your new element is not going to break your layout or cause trouble visually.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. The After() Method<\/h3>\n\n\n\n<p>In the same way that the <em>before()<\/em> method lets you insert elements before another indicated element, the <em>after()<\/em> method allows for the opposite. For example, it could be used to <a href=\"https:\/\/api.jquery.com\/after\/#after-content-content\">add an element right below<\/a> a button, once the button is selected:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(this).after( \"&lt;div class='square'&gt;Another square&lt;\/div&gt;\" );<\/pre>\n\n\n\n<p>Adding interactive content by manipulating the DOM with jQuery like this is a solid way to gain interactivity, without <a href=\"https:\/\/www.wearediagram.com\/blog\/the-benefits-of-jquery-in-front-end-website-development\">losing speed<\/a> in the process.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. The Append() Method<\/h3>\n\n\n\n<p>Instead of making individual new elements appear, you might want to add your new element onto an existing one. For instance, maybe you want to add the text \u201cHappy Birthday!\u201d within a colored area whenever a specific button is clicked on.<\/p>\n\n\n\n<p>To append your new element to the target element\u2019s existing content, you would insert the <em>append()<\/em> jQuery command within the <a href=\"https:\/\/www.quackit.com\/jquery\/tutorial\/jquery_dom_manipulation.cfm\">HTML source code<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$( \".box\" ).append( \"&lt;p&gt;Happy Birthday!&lt;\/p&gt;\" );<\/pre>\n\n\n\n<p>This method adds the manipulation onto the end of the element you indicate in the string, rather than putting it before or after. Within the context of the full HTML document, you can see how the manipulation takes place:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!doctype html&gt;<br>&lt;title&gt;Example&lt;\/title&gt;<br>&lt;style&gt;<br>.box {<br>&nbsp;&nbsp;background: orange;<br>&nbsp;&nbsp;color: white;<br>&nbsp;&nbsp;color: white;<br>&nbsp;&nbsp;width: 150px;<br>&nbsp;&nbsp;padding: 20px;<br>&nbsp;&nbsp;margin: 10px;<br>}<br>&lt;\/style&gt;<br>&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.1.1\/jquery.min.js\"&gt;&lt;\/script&gt;<br>&lt;script&gt;<br>&nbsp;&nbsp;$( function() {<br>&nbsp;&nbsp;&nbsp;&nbsp;$( \"button\" ).click( function() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(this).after( \"&lt;div class='box'&gt;Happy Birthday!&lt;\/div&gt;\" );<br>&nbsp;&nbsp;&nbsp;&nbsp;});<br>&nbsp;&nbsp;});<br>&lt;\/script&gt;<br>&lt;button&gt;Create a box&lt;\/button&gt;<\/pre>\n\n\n\n<p>This technique gives you a simple option for creating interactivity within a confined space. Just remember, with this particular method the manipulations will appear as an extension of the element you indicate, rather than as new individual elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. The Prepend() Method<\/h3>\n\n\n\n<p>As the balance to the <em>append()<\/em> method, <em>prepend()<\/em> will add an element to the end of whatever target element you indicate in the <a href=\"https:\/\/www.quackit.com\/jquery\/tutorial\/jquery_dom_manipulation.cfm\">jQuery command<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$( \".banner\" ).prepend( \"&lt;p&gt;Happy Birthday!&lt;\/p&gt;\" );<\/pre>\n\n\n\n<p>In this example, the text \u201cHappy Birthday!\u201d would appear at the beginning of the banner indicated in the code string.<\/p>\n\n\n\n<p>It\u2019s worth noting that these methods (including <em>before()<\/em>, <em>after()<\/em>, and <em>append()<\/em>) are all acceptable approaches for simple uses. However, we do recommend other techniques for more complex insertions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. The Clone() Method<\/h3>\n\n\n\n<p>Next up, the <em>clone()<\/em> method is a pretty powerful option in terms of manipulating the DOM with jQuery. While the previous four techniques enable you to insert elements and move them around, <em>clone()<\/em> makes it possible to copy an element, remove it from its original place, and reinsert or append it elsewhere.<\/p>\n\n\n\n<p>For example, this line means that the original box element will remain where it is, while a clone of it <a href=\"https:\/\/api.jquery.com\/clone\/#clone-withDataAndEvents\">will be appended<\/a> to the triangle element:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$( \".box\" ).clone().appendTo( \".triangle\" );<\/pre>\n\n\n\n<p>The downside to this method is that it can cause duplication of the element\u2019s <em>id<\/em> attribute. These attributes are meant to be unique. You can avoid this issue by using the <em>class<\/em> attributes as identifiers, and <a href=\"https:\/\/api.jquery.com\/clone\/\">employing the clone method sparingly<\/a>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. The Wrap() Method<\/h3>\n\n\n\n<p>Finally, the <em>wrap()<\/em> method is useful if you want to wrap an element around an existing string. For instance, <a href=\"https:\/\/api.jquery.com\/wrap\/#wrap-wrappingElement\">you can wrap several paragraphs<\/a> in a new HTML structure by identifying the class and indicating what structure is being implemented:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$( \".targetclass\" ).wrap( \"&lt;div class='new'&gt;&lt;\/div&gt;\" );<\/pre>\n\n\n\n<p>With this technique, you are creating a <em>&lt;div&gt;<\/em> around any element that matches the target class in the <em>wrap()<\/em> string. While you can create a wrap that is several levels deep, however, you should make sure there is only one innermost element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting the DOM<\/h2>\n\n\n\n<p>When it comes to working with the DOM, we want to highlight one key aspect you\u2019ll want to pay attention to. In order to keep your pages loading fast and avoid unnecessary code (especially if you\u2019re developing a theme), you should be mindful not to use any elements that are not part of the DOM tree.<\/p>\n\n\n\n<p>If you\u2019re not sure whether an element is part of the DOM or not, you can check each one <a href=\"https:\/\/developers.google.com\/web\/tools\/chrome-devtools\/dom\/\">through your browser<\/a> on the front end of your site. You can inspect elements on the page, and determine where they are included in the DOM tree.<\/p>\n\n\n\n<p>This is especially helpful when another script has changed the original HTML of your site. Otherwise, <a href=\"https:\/\/developers.google.com\/web\/tools\/chrome-devtools\/dom\/#appendix\">your HTML and DOM<\/a> may sometimes appear exactly the same.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Explore Other WP Engine Resources and Tools<\/h2>\n\n\n\n<p>If you think of the DOM as an organic part of your site, a living resource that responds to HTML source code, the power of its accessibility is pretty astounding. That\u2019s why understanding the DOM and how it can be used to enhance your projects can help take your site to the next level.&nbsp;<\/p>\n\n\n\n<p>Manipulating the DOM with jQuery highlights the beauty of WordPress\u2019 open-source platform. Here at WP Engine, we understand how high-quality <a href=\"https:\/\/developer.wordpress.org\/\">developer resources<\/a> can make all the difference. That\u2019s why we offer plenty of help for WordPres users, along with <a href=\"https:\/\/wpengine.com\/plans\/\">top-notch hosting plans<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thanks to WordPress\u2019 open-source platform and vibrant developer community, creating themes for the platform has a pretty wide appeal. Navigating all the elements and content required to build a WordPress site can be challenging, however, depending on your programming knowledge and experience. Fortunately, the Document Object Model (DOM) can make it easier to manipulate or<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":297,"featured_media":0,"template":"","resource-topic":[904],"resource-role":[895,896,906,897,903],"resource-type":[916],"class_list":["post-91376","resource","type-resource","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Manipulate DOM in WordPress | WP Engine<\/title>\n<meta name=\"description\" content=\"manipulating DOM within WordPress? Our guide gets into the steps you&#039;ll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Manipulate DOM in WordPress | WP Engine\" \/>\n<meta property=\"og:description\" content=\"manipulating DOM within WordPress? Our guide gets into the steps you&#039;ll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-29T00:13:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/\",\"url\":\"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/\",\"name\":\"How To Manipulate DOM in WordPress | WP Engine\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2019-09-05T19:12:08+00:00\",\"dateModified\":\"2023-06-29T00:13:13+00:00\",\"description\":\"manipulating DOM within WordPress? Our guide gets into the steps you'll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Manipulating DOM in WordPress using JQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521\",\"name\":\"Erin Myers\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"caption\":\"Erin Myers\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Manipulate DOM in WordPress | WP Engine","description":"manipulating DOM within WordPress? Our guide gets into the steps you'll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How To Manipulate DOM in WordPress | WP Engine","og_description":"manipulating DOM within WordPress? Our guide gets into the steps you'll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.","og_url":"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2023-06-29T00:13:13+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/","url":"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/","name":"How To Manipulate DOM in WordPress | WP Engine","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2019-09-05T19:12:08+00:00","dateModified":"2023-06-29T00:13:13+00:00","description":"manipulating DOM within WordPress? Our guide gets into the steps you'll need to take, from HTML to Jquery. Dive into our DOM WordPress guide.","breadcrumb":{"@id":"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/resources\/how-to-manipulate-dom-in-wordpress-with-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"Manipulating DOM in WordPress using JQuery"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521","name":"Erin Myers","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","caption":"Erin Myers"}}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2019\/09\/shutterstock_697413862.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Agency, Developer, Entrepreneur, Freelancer, Marketer","topic":"<strong>Topics:<\/strong> Marketing","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/91376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/297"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=91376"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=91376"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=91376"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=91376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}