<?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: Tygari</title>
    <description>The latest articles on DEV Community by Tygari (@tygari).</description>
    <link>https://dev.to/tygari</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%2F148812%2F54f14f85-d8f3-4591-8c72-c7d467724f14.png</url>
      <title>DEV Community: Tygari</title>
      <link>https://dev.to/tygari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tygari"/>
    <language>en</language>
    <item>
      <title>Echo-js</title>
      <dc:creator>Tygari</dc:creator>
      <pubDate>Sun, 04 Apr 2021 21:01:01 +0000</pubDate>
      <link>https://dev.to/tygari/echo-js-4om1</link>
      <guid>https://dev.to/tygari/echo-js-4om1</guid>
      <description>&lt;p&gt;I have chosen to write my first article about a tool I wrote.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/tygari/echo-js"&gt;Echo-js&lt;/a&gt; is a tool for creating / rearranging /deleting repetitive children within a parent element dynamically within the HTML DOM.&lt;/p&gt;

&lt;p&gt;In my coding, I kept coming across cases where I needed to quickly and easily write many repetitive element children, rearrange those children, and sometimes delete specific children.&lt;/p&gt;

&lt;p&gt;I had heard about some such tools within frameworks, but I was still working on learning the core language.  I wasn't ready to tackle tricky and confusing frameworks, yet nothing existed in the core language for what felt like a simple task.&lt;/p&gt;

&lt;p&gt;Over time I developed complex functions to accomplish this but they were limited and had many issues.  As I learned more about programming I was able to refine those functions into simpler and more efficient scripts.  Then I discovered the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components"&gt;Web Components&lt;/a&gt; API within core Javascript and wrote &lt;a href="https://github.com/tygari/echo-js/tree/wc"&gt;Echo-js version 1&lt;/a&gt;.  I wrote a web component to do the job of the above but it required using a specific tag which still felt wrong.&lt;/p&gt;

&lt;p&gt;After a year, I discovered the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver"&gt;MutationObserver&lt;/a&gt; API within core Javascript to fix this issue.  With this, I was able to solve the tag issue and allow the Echo-js tool to work with any tag. So I then wrote &lt;a href="https://github.com/tygari/echo-js/tree/master"&gt;Echo-js version 2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Add this script code to ones page to make use of the Echo-js tool.&lt;br&gt;
&lt;code&gt;&amp;lt;script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tygari/echo-js/echo.js" sync&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Echo-js uses two new attributes currently not in use by any core HTML tag or API.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;echo&lt;/strong&gt; attribute takes in a string of ID's or a numeric value and creates children based on the &lt;strong&gt;code&lt;/strong&gt; attribute.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;code&lt;/strong&gt; attribute takes in a string of HTML code that is then used as the template dictated by the &lt;strong&gt;echo&lt;/strong&gt; attribute.  When not assigned an HTML string or assigned an invalid HTML string this attribute will default to &lt;code&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the start, Echo-js only took in a string of ID's assigned to the &lt;strong&gt;echo&lt;/strong&gt; attribute and a string of HTML code assigned to the &lt;strong&gt;code&lt;/strong&gt;.  Over time I added more and more features to give this tool more versatility.  As I found bugs I fixed them.  Over time I refined the code to make it more efficient.&lt;/p&gt;

&lt;p&gt;Echo-js, when given a string of words, will create a child for each word and apply each word as an ID to those children in the order of the words from left to right.&lt;/p&gt;

&lt;p&gt;Written HTML&lt;br&gt;
&lt;code&gt;&amp;lt;div id="test" echo="bar foo barfoo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="bar foo barfoo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;
    &amp;lt;span id="bar"&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span id="foo"&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span id="barfoo"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The children can be targeted and altered.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById('bar').style.color = 'red';&lt;br&gt;
document.getElementById('bar').innerText = 'red';&lt;br&gt;
document.getElementById('foo').style.color = 'yellow';&lt;br&gt;
document.getElementById('foo').innerText = 'yellow';&lt;br&gt;
document.getElementById('barfoo').style.color = 'blue';&lt;br&gt;
document.getElementById('barfoo').innerText = 'blue';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="bar foo barfoo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;
    &amp;lt;span id="bar" style="color:red"&amp;gt;red&amp;lt;/span&amp;gt;
    &amp;lt;span id="foo" style="color:yellow"&amp;gt;yellow&amp;lt;/span&amp;gt;
    &amp;lt;span id="barfoo" style="color:blue"&amp;gt;blue&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that each span is individualized, time to alter the echo attribute by rearranging the order of its words.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("echo", "barfoo bar foo");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="barfoo bar foo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;
    &amp;lt;span id="barfoo" style="color:blue"&amp;gt;blue&amp;lt;/span&amp;gt;
    &amp;lt;span id="bar" style="color:red"&amp;gt;red&amp;lt;/span&amp;gt;
    &amp;lt;span id="foo" style="color:yellow"&amp;gt;yellow&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Echo-js will copy all the children, then delete them from the HTML DOM, then paste them back in the new order.  It will preserve any unique alterations applied to each child.&lt;/p&gt;

&lt;p&gt;Even adding another word to the list won't harm the already present children.  In the order position of the new word string, a new child will be created based on the &lt;strong&gt;code&lt;/strong&gt; attribute.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("echo", "barfoo bar foobar foo");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="barfoo bar foobar foo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;
    &amp;lt;span id="barfoo" style="color:blue"&amp;gt;blue&amp;lt;/span&amp;gt;
    &amp;lt;span id="bar" style="color:red"&amp;gt;red&amp;lt;/span&amp;gt;
    &amp;lt;span id="foobar"&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span id="foo" style="color:yellow"&amp;gt;yellow&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose one decides they no longer desire a specific child.  Simply removing the name from the &lt;strong&gt;echo&lt;/strong&gt; attribute word string will delete it from the HTML DOM.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("echo", "bar foobar foo");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="bar foobar foo" code="&amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;"&amp;gt;
    &amp;lt;span id="bar" style="color:red"&amp;gt;red&amp;lt;/span&amp;gt;
    &amp;lt;span id="foobar"&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span id="foo" style="color:yellow"&amp;gt;yellow&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A word of warning, altering the &lt;strong&gt;code&lt;/strong&gt; attribute wipes everything clean, then reapplies &lt;strong&gt;echo&lt;/strong&gt; from a fresh start.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("code", "&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="bar foobar foo" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;
    &amp;lt;p id="bar"&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p id="foobar"&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p id="foo"&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the programmer simply needs a set number of children without any ID's, they may use a number instead of a word string.  &lt;/p&gt;

&lt;p&gt;Written HTML&lt;br&gt;
&lt;code&gt;&amp;lt;div id="test" echo="5" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="5" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to number these children.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById('test').children[0].innerText = '1';&lt;br&gt;
document.getElementById('test').children[1].innerText = '2';&lt;br&gt;
document.getElementById('test').children[2].innerText = '3';&lt;br&gt;
document.getElementById('test').children[3].innerText = '4';&lt;br&gt;
document.getElementById('test').children[4].innerText = '5';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="5" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;
    &amp;lt;p&amp;gt;1&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;2&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;3&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;4&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;5&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing the number to a higher number will result in new children being added, without losing any uniqueness of the older children.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("echo", "8");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="8" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;
    &amp;lt;p&amp;gt;1&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;2&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;3&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;4&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;5&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the inverse reducing the number to a smaller number will simply delete the excess children.  It will retain the children's uniqueness below the number.&lt;/p&gt;

&lt;p&gt;Javascript&lt;br&gt;
&lt;code&gt;document.getElementById("test").setAttribute("echo", "3");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOM HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="test" echo="3" code="&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;"&amp;gt;
    &amp;lt;p&amp;gt;1&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;2&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;3&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the simpler capabilities of this tool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Echo&lt;/strong&gt; attribute can be assigned an array and it will process it directly.&lt;br&gt;
&lt;strong&gt;Echo&lt;/strong&gt; attribute can be assigned a variable and it will find it within the global scape and retrieve the data.&lt;br&gt;
If assigned a variable an &lt;strong&gt;auto&lt;/strong&gt; attribute can be set to watch the variable and Echo-js will periodically check the variable for changes.  When it detects a change it will auto update the &lt;strong&gt;echo&lt;/strong&gt; attribute to match the altered variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt; attribute can, besides a string of HTML code,, accept directions to an HTML Template to retrieve code or a variable like &lt;strong&gt;echo&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are many options for a programmer to set the tool to work in a way most efficient to them.&lt;/p&gt;

&lt;p&gt;The README details every aspect of the tool.  It gives examples of every potential option.&lt;/p&gt;

&lt;p&gt;My personal, most audacious use of this tool has been with a simple single HTML line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div id="twodarray" echo="1000" code="&amp;lt;div echo='1000' code='&amp;lt;div onclick=()&amp;gt;&amp;lt;/div&amp;gt;'&amp;gt;&amp;lt;/div&amp;gt;" auto="false"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Echo.js was able to successfully create a 2D array of 1000 children each holding 1000 children for 1001001 total elements of parent, children, and grandchildren.&lt;/p&gt;




&lt;p&gt;Any suggestions or feedback would be much appreciated.&lt;/p&gt;

&lt;p&gt;Please report any bugs or unusual behavior one may experience so a patch may be applied as soon as possible for all who may use this tool.&lt;/p&gt;

</description>
      <category>html</category>
      <category>attributes</category>
      <category>echo</category>
      <category>code</category>
    </item>
  </channel>
</rss>
