<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ZediWards</title>
    <description>The latest articles on DEV Community by ZediWards (@zediwards).</description>
    <link>https://dev.to/zediwards</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F115589%2Ff3dfcc33-1819-41be-9417-d6ec7dc32950.jpeg</url>
      <title>DEV Community: ZediWards</title>
      <link>https://dev.to/zediwards</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zediwards"/>
    <language>en</language>
    <item>
      <title>Moving a static HTML element into a dynamically created element with JavaScript. </title>
      <dc:creator>ZediWards</dc:creator>
      <pubDate>Wed, 30 Jun 2021 01:59:27 +0000</pubDate>
      <link>https://dev.to/zediwards/moving-a-static-html-element-into-an-dynamically-created-element-2570</link>
      <guid>https://dev.to/zediwards/moving-a-static-html-element-into-an-dynamically-created-element-2570</guid>
      <description>&lt;h2&gt;
  
  
  Context:
&lt;/h2&gt;

&lt;p&gt;I was building a series of modals that open separately when their corresponding button was clicked. Each modal was split in two parts. The left side was a SVG of a tank, and the right, a form with some inputs and a update button. &lt;/p&gt;

&lt;p&gt;The tank's SVG fill gradient would change depending on the inputs entered in the form to its right.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0523oz3k0zjyj97vmxp9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0523oz3k0zjyj97vmxp9.png" alt="tank-1-modal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was coding all of this out in Javascript, using ALOT of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt; &lt;span class="c1"&gt;// Practicing my DOM skills :)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When it came time to recreate the SVG in JavaScript, it became a bit much. Especially since I had six to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3j1g1zj3t83amvf04zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3j1g1zj3t83amvf04zj.png" alt="Component 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML &amp;amp; CSS
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Code the SVG inside the HTML parent that the dynamically created modal will be appended to later.

&lt;ul&gt;
&lt;li&gt;The SVG being placed inside the modals parent will make it easier to traverse the DOM later and place it inside the modal.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Add a CSS class of hidden to the SVG element.
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.hidden&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;On the lines after I created the modal&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grab the SVG by its class name&lt;/li&gt;
&lt;li&gt;Grab the element that will be the SVGs new parent

&lt;ul&gt;
&lt;li&gt;target it by traversing the DOM in relation to the SVG elements current location&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Append the SVG to the new parent&lt;/li&gt;

&lt;li&gt;Remove the hidden class
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;svg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`svg-tank-1`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;svgLocationTarget&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;svg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextElementSibling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;svgLocationTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;svg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;svg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  TADA!!!
&lt;/h3&gt;

&lt;p&gt;I took a hard coded HTML element and relocated it inside a dynamically created element. Then I made it visible by removing the "hidden" class name. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
