<?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>Forem: Steve P.</title>
    <description>The latest articles on Forem by Steve P. (@stevepenner).</description>
    <link>https://forem.com/stevepenner</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%2F296697%2F09ca1e8a-c684-47d7-80fc-30e4cbe071fd.jpg</url>
      <title>Forem: Steve P.</title>
      <link>https://forem.com/stevepenner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stevepenner"/>
    <language>en</language>
    <item>
      <title>Keeping HTML and JavaScript (and CSS) Fully Separated</title>
      <dc:creator>Steve P.</dc:creator>
      <pubDate>Thu, 21 Mar 2024 14:53:52 +0000</pubDate>
      <link>https://forem.com/stevepenner/keeping-html-and-javascript-and-css-fully-separated-m6m</link>
      <guid>https://forem.com/stevepenner/keeping-html-and-javascript-and-css-fully-separated-m6m</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explainer
&lt;/h2&gt;

&lt;p&gt;In a total very useful separation of concerns, all devs can and should keep all JavaScript and CSS out of HTML markup--not using "on-" event handlers--using DOM element 'id' attribute, largely using the always handy &lt;code&gt;document.getElementById&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Context
&lt;/h2&gt;

&lt;p&gt;Do web devs still define event handlers in HTML markup, written in an element attribute?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/myEntryPage.html
&amp;lt;input type="radio" name="colorSelect" value="red" onchange="selectColor(this);"&amp;gt; Red
&amp;lt;input type="radio" name="colorSelect" value="blue" onchange="selectColor(this);"&amp;gt; Blue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead all devs should use the HTML &lt;code&gt;id&lt;/code&gt; attribute (rarely the &lt;code&gt;class&lt;/code&gt; attribute when needed) along with the JavaScript DOM methods &lt;code&gt;document.getElementById()&lt;/code&gt; and &lt;code&gt;document.addEventListener()&lt;/code&gt; to set up document elements with event handlers, rather than using "onclick=" or "onchange=" (or any "on-") element attributes. JavaScript (and particularly the highly recommended TypeScript) is not usually subject to error/code-checking if using script of any kind defined as an element attribute or even with multiple line/statements enclosed in &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags. The above markup has been re-coded below to fully remove JavaScript from HTML markup and put it properly into &lt;code&gt;*.js&lt;/code&gt; (or &lt;code&gt;*.ts&lt;/code&gt;) files. The script shown is TypeScript (highly recommended) so the event handlers are initialized on page load.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/myEntryPage.html
&amp;lt;input type="radio" id="colorSelect-red" value="red"&amp;gt; Red
   &amp;lt;!-- id="colorSelect-red" CHANGE="selectColor(this);" --&amp;gt;
&amp;lt;input type="radio" id="colorSelect-blue" value="blue" onchange="selectColor(this);"&amp;gt; Blue
   &amp;lt;!-- id="colorSelect-blue" CHANGE="selectColor(this);" --&amp;gt;

/myEntryScript.ts
document.addEventListener("DOMContentLoaded", (/* event: Event */) =&amp;gt; {
   document.getElementById("colorSelect-red").addEventListener("change", (event: Event) =&amp;gt; {
      selectColor(event.target as HTMLInputElement);
   });
   document.getElementById("colorSelect-blue").addEventListener("change", (event: Event) =&amp;gt; {
      selectColor(event.target as HTMLInputElement);
   });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Note, to track elements in markup with event handlers, you might put an HTML comment underneath with the 'id' of the element and the event CAPITALIZED with its code as an aid in documenting/annotating.)&lt;/p&gt;

</description>
      <category>frontendchallenge</category>
      <category>devchallenge</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Job Posting Question: If you have your way, how would you design a chicken?</title>
      <dc:creator>Steve P.</dc:creator>
      <pubDate>Fri, 24 Jan 2020 21:14:41 +0000</pubDate>
      <link>https://forem.com/stevepenner/job-posting-question-if-you-have-your-way-how-would-you-design-a-chicken-38om</link>
      <guid>https://forem.com/stevepenner/job-posting-question-if-you-have-your-way-how-would-you-design-a-chicken-38om</guid>
      <description>&lt;p&gt;The job announcement is for a web application developer with knowledge of front-end development languages to be used in the state government enterprise.&lt;/p&gt;

&lt;p&gt;This has to be a trick question, right?&lt;/p&gt;

&lt;p&gt;Or is it what I expect, where I go into a concise answer making sure I use all the important terms: class, interface, properties, methods, encapsulation, inheritance, abstraction, polymorphism?&lt;/p&gt;

</description>
      <category>career</category>
    </item>
  </channel>
</rss>
