<?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: Spencer</title>
    <description>The latest articles on DEV Community by Spencer (@kapenike).</description>
    <link>https://dev.to/kapenike</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%2F1423528%2F7c4964d5-0404-48f3-b7d9-e2bd3faf6b13.png</url>
      <title>DEV Community: Spencer</title>
      <link>https://dev.to/kapenike</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kapenike"/>
    <language>en</language>
    <item>
      <title>Javascript, tiny functional helpers for modifying the DOM</title>
      <dc:creator>Spencer</dc:creator>
      <pubDate>Sat, 13 Apr 2024 23:53:27 +0000</pubDate>
      <link>https://dev.to/kapenike/javascript-tiny-functional-helpers-for-modifying-the-dom-5792</link>
      <guid>https://dev.to/kapenike/javascript-tiny-functional-helpers-for-modifying-the-dom-5792</guid>
      <description>&lt;p&gt;Hey all o/, I'm a vanilla JS nut who's recently been on a functional style programming kick. It's been great in methods ... until it came time to modifying elements in the DOM.&lt;/p&gt;

&lt;p&gt;It doesn't seem like there is a good way to approach element creation and manipulation with my favorite functions: &lt;code&gt;.map(), .forEach(), .reduce()&lt;/code&gt;, etc... I know jQuery accomplishes this but I think it's just too much! I don't want anything other than to easily work with HTML elements.&lt;/p&gt;

&lt;p&gt;So I made a way :D&lt;/p&gt;

&lt;p&gt;I've created three helper functions: &lt;code&gt;Create(), Select()&lt;/code&gt;and&lt;code&gt;MSelect()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create('node name') creates an html element&lt;/li&gt;
&lt;li&gt;Select('#___') selects a single node&lt;/li&gt;
&lt;li&gt;MSelect('.___') selects a list of nodes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each helper function takes an &lt;code&gt;object&lt;/code&gt; as a second parameter to assign native properties to during creation or selection. All functions return the element or elements after creation or modification. &lt;strong&gt;That's all it does.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.body.appendChild(
    Create('div', {
        children: [
            Create('h1', {
                innerHTML: 'Welcome to JSUI',
                id: 'title',
                style: {
                    color: '#222222'
                }
            }),
            Create('p', {
                innerHTML: 'Why would you use this?'
            }),
            Create('ul', {
                id: 'my_list',
                children: [
                    'It allows a functional approach to element creation, selection and manipulation',
                    'Its absolutely tiny',
                    'Its not magic. It just assigns properties and children using functional methods'
                ].map(x =&amp;gt; {
                    return Create('li', {
                        innerHTML: x,
                        className: 'my_list_elements',
                        onclick: function () {
                            console.log('Here is my list:');
                            console.log(Select('#my_list'));
                            console.log('Now I make the list elements bold');
                            MSelect('.my_list_elements', {
                                style: {
                                    fontWeight: 'bold'
                                }
                            });
                        }
                    })
                })
            })
        ]
    })
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Minified code:&lt;br&gt;
&lt;code&gt;var Select=function(e,t={}){return 0===Object.keys(t).length?document.querySelector(e):Create(document.querySelector(e),t,!0)},MSelect=function(e,t={}){return 0===Object.keys(t).length?document.querySelectorAll(e):document.querySelectorAll(e).forEach((e=&amp;gt;Create(e,t,!0)))||document.querySelectorAll(e)},Create=function(e,t={},r=!1){return Object.keys(t).reduce(((e,r)=&amp;gt;Array.isArray(t[r])?t[r].forEach((t=&amp;gt;e.appendChild(t)))||e:"object"==typeof t[r]?null&amp;amp;Object.assign(e[r],t[r])||e:Object.assign(e,{[r]:t[r]})),r?e:document.createElement(e))};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Github repo if you're interested in checking it out!&lt;br&gt;
&lt;a href="https://github.com/kapenike/JSUI/"&gt;https://github.com/kapenike/JSUI/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is a helper function and not a library, it doesn't protect you, everything is as ordered. If you assign &lt;code&gt;children&lt;/code&gt; before you assign &lt;code&gt;innerHTML&lt;/code&gt; .... the children will be overridden because well ... thats how Javascript works. Make sure to define properties in logical order in an edge case like this.&lt;/p&gt;

&lt;p&gt;Happy coding :D&lt;/p&gt;

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