{"id":1927,"date":"2024-05-08T08:41:57","date_gmt":"2024-05-08T14:41:57","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=1927"},"modified":"2024-05-08T08:41:58","modified_gmt":"2024-05-08T14:41:58","slug":"5-things-designers-can-do-with-javascript","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/5-things-designers-can-do-with-javascript\/","title":{"rendered":"Five Basic Things About JavaScript That Will Help Non JavaScript-Focused Web Designers"},"content":{"rendered":"\n<p>Let&#8217;s say you don&#8217;t know JavaScript. <\/p>\n\n\n\n<p>Like, at all. <\/p>\n\n\n\n<p>You haven&#8217;t needed it. You&#8217;re a web designer and you&#8217;re focused largely on UI and UX. Your skills with design tools, HTML, CSS, and team communication have served you well.<\/p>\n\n\n\n<p>But you know what JavaScript <em>is<\/em>. It&#8217;s another part of the native web. It&#8217;s powerful. It can make websites <em>do stuff<\/em> beyond what HTML and CSS can do alone. You don&#8217;t even care to <em>deeply<\/em> learn JavaScript, you just want to get a ton of <strong>bang for the buck<\/strong> and learn things that will help you do your existing job better.<\/p>\n\n\n\n<p>Let&#8217;s look at some things you could learn in a day that will give you that bang for the buck.<\/p>\n\n\n\n<div class=\"wp-block-group learn-more\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>We&#8217;re going to look at JavaScript code from here on out. But just so I don&#8217;t lose anyone, let me make clear <em>where<\/em> you would put this JavaScript code to make it work. You&#8217;re put it at the bottom of the HTML of the page you&#8217;re working on, before the closing <code>&lt;\/body&gt;<\/code> tag, put a <code>&lt;script&gt; \/* you're code here *\/ &lt;\/script&gt;<\/code>. That code will run when any URL that uses that HTML loads. <\/p>\n\n\n\n<p>If you&#8217;re writing a lot of it, or want to apply it to multiple HTML documents, you could also do <code>&lt;script src=\".\/script.js\"&gt;&lt;\/script&gt;<\/code> and put the JavaScript code in there. That references a file called <code>script.js<\/code> that you would make and put next to the HTML. <\/p>\n\n\n\n<p>Also, a lot of this code will run in the DevTools console in any of the browsers, so you could copy and paste it into there and hit Enter to try it.<\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">1) Learn to Select Things<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"705\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?resize=1024%2C705&#038;ssl=1\" alt=\"\" class=\"wp-image-2089\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?resize=1024%2C705&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?resize=300%2C206&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?resize=768%2C528&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?resize=1536%2C1057&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-07-at-11.03.13%E2%80%AFAM.png?w=2026&amp;ssl=1 2026w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>In JavaScript, you often want what&#8217;s called a <em>reference<\/em> to an element. That is, a variable that represents the element in the DOM you&#8217;re trying to do something with. <\/p>\n\n\n\n<p>Believe it or not, you&#8217;re at a real advantage here. Since you already know some CSS, those selectors are exactly what JavaScript can use to get it&#8217;s hands on those elements.<\/p>\n\n\n\n<p>Say you had HTML like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"site-header\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"logo\"<\/span>&gt;<\/span>My Website<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you wanted a reference to that <code>&lt;header&gt;<\/code> element, do it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> header = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\"#site-header\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>\"string\"<\/code> inside that <code>querySelector()<\/code> function is essentially the same as the CSS selector <code>#site-header<\/code>. <\/p>\n\n\n\n<p>If you wanted the logo, you could do:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> logo = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\".logo\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You might be thinking to yourself: but CSS classes might apply to <em>lots<\/em> of elements, how does it know which one to get? The <code>querySelector<\/code> function will select the <em>first<\/em> one it finds as it starts looking from the top of the HTML. If you intentionally want to look for and get a reference to a whole set of elements, look at <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelectorAll\">querySelectorAll()<\/a>;<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2) Learn to Update Classes (i.e. Toggle Stuff)<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"736\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?resize=1024%2C736&#038;ssl=1\" alt=\"\" class=\"wp-image-2090\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?resize=1024%2C736&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?resize=300%2C216&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?resize=768%2C552&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?resize=1536%2C1105&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.05.42%402x.png?w=2022&amp;ssl=1 2022w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>#1 is a big concept, but it doesn&#8217;t actually <em>do<\/em> anything, it just helps you get those element references. Now let&#8217;s enact a real <em>change<\/em>. Let&#8217;s add a class to that header element we&#8217;ve got.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">header.classList.add(<span class=\"hljs-string\">\"dark\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That does exactly what it looks like. It adds a class to that <code>&lt;header&gt;<\/code> element, so now the HTML (the &#8220;DOM&#8221;, really) will be like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"site-header\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"dark\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"logo\"<\/span>&gt;<\/span>My Website<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As an HTML and CSS person, I imagine you can feel the power here. You can change <em>any styling you want<\/em> when you&#8217;ve got a class name you can select and use.<\/p>\n\n\n\n<p>In fact, think about how you can change classes all the way up on the <code>&lt;html&gt;<\/code> element. A class there means, through the power of CSS, <strong>you can change the style for <em>anything<\/em> on the page.<\/strong> As a neat bonus, you don&#8217;t even have to <code>querySelector<\/code> for the <code>&lt;html&gt;<\/code> element. JavaScript automatically has a reference to it available.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">document<\/span>.documentElement.classList.add(<span class=\"hljs-string\">\"paying-user\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/classList\"><code>classList<\/code> API<\/a> allows you to add, remove, replace, and toggle classes, as well as check to see if an element contains a certain class, so you&#8217;ve got a lot of control there. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">element.classList.add(<span class=\"hljs-string\">\"javascript-enabled\"<\/span>);\nelement.classList.remove(<span class=\"hljs-string\">\"loading\"<\/span>);\nelement.classList.replace(<span class=\"hljs-string\">\"cat-lover\"<\/span>, <span class=\"hljs-string\">\"dog-lover\"<\/span>);\nelement.classList.toggle(<span class=\"hljs-string\">\"open\"<\/span>);\nelement.classList.toggle(<span class=\"hljs-string\">\"raining\"<\/span>, weather === <span class=\"hljs-string\">\"rain\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">3) Learn to Listen for Events (e.g. Scroll, Click, and Change)<\/h2>\n\n\n\n<p>Selecting elements and changing classes is powerful, but it&#8217;s very likely you&#8217;re going to want to do that <em>when<\/em> something happens. The <em>when<\/em> in JavaScript is often an <strong>event<\/strong>. It&#8217;s another API that allows us to &#8220;listen&#8221; for events. Events are happening all the time! It&#8217;s our job to respond to them when they happen.<\/p>\n\n\n\n<p>The <code>click<\/code> event is the ultimate classic. You&#8217;ll want to attach <code>click<\/code> events to elements that can receive focus. A <code>&lt;button&gt;<\/code>, for example, is perfect. You <em>can<\/em> attach a click &#8220;handler&#8221; to a <code>&lt;div&gt;<\/code>, but it&#8217;s a bad choice as a <code>&lt;div&gt;<\/code> isn&#8217;t focusable. Many users use their keyboard only to navigate the web and may rely on screen readers to interact with it, and would be unable to &#8220;click&#8221; a <code>&lt;div&gt;<\/code> with a click hander.<\/p>\n\n\n\n<p>Imagine our header has a button in it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"site-header\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"logo\"<\/span>&gt;<\/span>My Website<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span>&gt;<\/span>Switcheroo<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>First we get a reference to that button, then add our click handling function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> button = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\"button\"<\/span>);\n\nbutton.addEventListener(<span class=\"hljs-string\">\"click\"<\/span>, () =&gt; {\n  <span class=\"hljs-comment\">\/* Code in here will run when the button is clicked *\/<\/span>\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">\"Button was clicked!\"<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Hopefully that code above is clear. We selected the button, and we added an event listener for the event type <code>click<\/code>. Don&#8217;t worry much about the rest of the syntax for now, but just know that code within those <code>{ }<\/code> &#8220;curly braces&#8221; will run when that button is clicked. <\/p>\n\n\n\n<p>I slipped a little bonus in there for you: <code>console.log()<\/code>. This little beautiful function allows you to send messages to the browser DevTools. If I have those DevTools open (Cmd\/Control &#8211; Option &#8211; J) I can see the message output there, which is a nice &#8220;sanity check&#8221; that things are working.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"712\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?resize=1024%2C712&#038;ssl=1\" alt=\"\" class=\"wp-image-2023\" style=\"width:662px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?resize=1024%2C712&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?resize=300%2C209&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?resize=768%2C534&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?resize=1536%2C1068&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-01-at-3.23.40%E2%80%AFPM.png?w=1916&amp;ssl=1 1916w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">You can send just about anything to <code>console.log()<\/code> inside those parenthesis. Try selecting an element and putting the variable name in there.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Inside our new <code>{ }<\/code> in that &#8220;click handler function&#8221;, we could do the job we already know how to do: update a class.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_bGJyzxa\" src=\"\/\/codepen.io\/anon\/embed\/bGJyzxa?height=450&amp;theme-id=47434&amp;slug-hash=bGJyzxa&amp;default-tab=js,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed bGJyzxa\" title=\"CodePen Embed bGJyzxa\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>I know that end result feels rather basic, but I hope it demystifies JavaScript a bit for you. In a sense, we&#8217;re just doing these three things:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select<\/li>\n\n\n\n<li>Listen<\/li>\n\n\n\n<li>Update<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">4) Change HTML<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"704\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?resize=1024%2C704&#038;ssl=1\" alt=\"\" class=\"wp-image-2091\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?resize=1024%2C704&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?resize=300%2C206&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?resize=768%2C528&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?resize=1536%2C1056&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/CleanShot-2024-05-07-at-11.08.06%402x.png?w=2024&amp;ssl=1 2024w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Changing the <code>class<\/code> of an element <em>is<\/em> changing the HTML of that element. But we&#8217;re not limited to that! We can quite literally change anything about it. Let&#8217;s assume <code>element<\/code> is a reference to an element like we learned in #1. We could:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">element.innerHTML = <span class=\"hljs-string\">\"&lt;div&gt;I'm now the entire guts of the element!!!&lt;\/div&gt;\"<\/span>;\n\nelement.remove(); <span class=\"hljs-comment\">\/\/ Totally gone<\/span>\n\nelement.dataset.version = <span class=\"hljs-string\">\"1.0.0\"<\/span>; <span class=\"hljs-comment\">\/\/ &lt;element data-version=\"1.0.0\"&gt;<\/span>\n\nelement.style.backgroundColor = <span class=\"hljs-string\">\"red\"<\/span>; <span class=\"hljs-comment\">\/\/ &lt;element style=\"background-color: red;\"&gt;<\/span>\n\nelement.insertAdjacentHTML(<span class=\"hljs-string\">\"afterbegin\"<\/span>, <span class=\"hljs-string\">'&lt;i&gt;Hello&lt;\/i&gt;'<\/span>); <span class=\"hljs-comment\">\/\/ &lt;element&gt;&lt;i&gt;Hello&lt;\/i&gt; ... <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Those are just some of the many powerful things you can do to HTML once you grab ahold and get a reference to an element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5) Grab the values out of Form Elements<\/h2>\n\n\n\n<p>We already know how to select elements, and it&#8217;s no different for form elements. Say we have a form element like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n  Size\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"range\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"size\"<\/span> <span class=\"hljs-attr\">...<\/span> &gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let&#8217;s:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select it<\/li>\n\n\n\n<li>Watch it for changes<\/li>\n\n\n\n<li>Do something when it does<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ 1<\/span>\n<span class=\"hljs-keyword\">const<\/span> sizeSlider = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\"#size\"<\/span>);\n\n<span class=\"hljs-comment\">\/\/ 2<\/span>\nsizeSlider.addEventListener(<span class=\"hljs-string\">\"input\"<\/span>, (e) =&gt; {\n\n  <span class=\"hljs-comment\">\/\/ 3<\/span>\n  wrapper.style.border = <span class=\"hljs-string\">` <span class=\"hljs-subst\">${e.target.value}<\/span>px solid black`<\/span>;\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now that you can grab stuff from naturally interactive elements on the page, that should open some doors!<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_NWVKwda\" src=\"\/\/codepen.io\/anon\/embed\/NWVKwda?height=450&amp;theme-id=47434&amp;slug-hash=NWVKwda&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed NWVKwda\" title=\"CodePen Embed NWVKwda\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>As an exercise, consider a password field like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"form\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n    Password\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"password\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"password\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can watch when the form is submitted, then check the password.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> form = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\"#form\"<\/span>);\n<span class=\"hljs-keyword\">const<\/span> passwordInput = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">\"#password\"<\/span>);\n\nform.addEventListener(<span class=\"hljs-string\">\"submit\"<\/span>, (e) =&gt; {\n  e.preventDefault();\n\n  <span class=\"hljs-keyword\">if<\/span> (passwordInput.value === <span class=\"hljs-string\">\"\"<\/span>) {\n    <span class=\"hljs-comment\">\/\/ User submitted empty password<\/span>\n  } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (passwordInput.value === <span class=\"hljs-string\">\"password\"<\/span>) {\n    <span class=\"hljs-comment\">\/\/ User submitted correct password<\/span>\n  } <span class=\"hljs-keyword\">else<\/span> {\n    <span class=\"hljs-comment\">\/\/ User submitted incorrect password<\/span>\n  }\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>What UI\/UX things might you want to do when doing this kind of interactive work? Perhaps you could &#8220;shake&#8221; the form on incorrect passwords. Maybe you could insert some HTML telling them the password was incorrect and they have X tries left. Maybe you could change colors. As a designer, you&#8217;ll likely know exactly what you want to do, and now the page is interactive enough to properly prototype it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus: Design mode<\/h2>\n\n\n\n<p>This is a little bonus one:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">document<\/span>.designMode = <span class=\"hljs-string\">\"on\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is probably most appropriate as a snippet to put in the DevTools console as a one-off, as having it <em>always<\/em> on would be weird. When this &#8220;mode&#8221; is on, every element on the page is editable. The text content of it, anyway.<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='320' src='https:\/\/videopress.com\/embed\/aaLskHgW?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>More:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you want parts of a page to be editable (that aren&#8217;t already form elements), you can always put <code>contentEditable<\/code> on them. <\/li>\n\n\n\n<li>Just poking around and changing stuff like styles and content in DevTools is essentially doing the same things (behind the scenes) as we&#8217;ve learned so far: manipulating the page with JavaScript. <\/li>\n\n\n\n<li>If you&#8217;re <em>really<\/em> into editing websites as they are in the browser as a design tool, check out more elaborate tools like <a href=\"https:\/\/chromewebstore.google.com\/detail\/visbug\/cdockenadnadldjbbgcallicgledbeoc?pli=1\">VisBug<\/a>. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Extra Credit: HTML Web Components<\/h2>\n\n\n\n<p>A Web Component (can be) an element in HTML you just&#8230; make up. The point of them is that they do something useful and you re-use them all over your site as needed. The name in them just has to have at least one dash:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">add-rainbows<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    HTML in here that doesn't *need* rainbows, but would like them.\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">add-rainbows<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>These are a native part of the web platform and can be used no matter what other technology you use to build the site, making them universally useful. If you learn this, which I feel are just as useful for purely UI reasons as they are for anything else, you can bring the idea of components to your HTML work just as you likely already do in your design work. Designers building re-usable components is a world I&#8217;d like to see.<\/p>\n\n\n\n<p>Here&#8217;s a silly example where the only job this Web Component has is to inject a <code>&lt;style><\/code> tag into itself that styles itself.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_pomzrKv\" src=\"\/\/codepen.io\/anon\/embed\/pomzrKv?height=450&amp;theme-id=47434&amp;slug-hash=pomzrKv&amp;default-tab=js,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed pomzrKv\" title=\"CodePen Embed pomzrKv\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Think: &#8220;A small, re-usable bit of styling and\/or functionality that wraps a bit of otherwise perfectly usable HTML&#8221;. Jeremy Keith <a href=\"https:\/\/adactio.com\/journal\/tags\/webcomponents\">listed some examples<\/a> recently:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Jason\u2019s\u00a0<a href=\"https:\/\/github.com\/jgarber623\/aria-collapsible\"><code>aria-collapsable<\/code><\/a>\u00a0for toggling the display of one element when you click on another.<\/li>\n\n\n\n<li>David\u2019s\u00a0<a href=\"https:\/\/github.com\/daviddarnes\/play-button\"><code>play-button<\/code><\/a>\u00a0for adding a play button to an\u00a0<code>audio<\/code>\u00a0or\u00a0<code>video<\/code>\u00a0element.<\/li>\n\n\n\n<li>Chris\u2019s\u00a0<a href=\"https:\/\/gomakethings.com\/html-web-components\/\"><code>ajax-form<\/code><\/a>\u00a0for sending a form via Ajax instead of a full page refresh.<\/li>\n\n\n\n<li>Jim\u2019s\u00a0<a href=\"https:\/\/blog.jim-nielsen.com\/2023\/html-web-components-an-example\/\"><code>user-avatar<\/code><\/a>\u00a0for adding a tooltip to an image.<\/li>\n\n\n\n<li>Zach\u2019s\u00a0<a href=\"https:\/\/github.com\/zachleat\/table-saw\"><code>table-saw<\/code><\/a>\u00a0for making tables responsive.<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Discussion<\/h2>\n\n\n\n<p>Are you in this position yourself? Was this helpful? Or are there other things in JavaScript that you&#8217;d prefer to learn? Have you gone through this yourself already? What kind of things were the very first things you learned in JavaScript that were helpful?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you don&#8217;t know JavaScript. You&#8217;re a web designer and you&#8217;re focused largely on UI and UX. Let&#8217;s look at some things you could learn in a day that will give you that bang for the buck.<\/p>\n","protected":false},"author":1,"featured_media":2087,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[24,3],"class_list":["post-1927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-design","tag-javascript"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/designer-thumb.jpg?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=1927"}],"version-history":[{"count":41,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1927\/revisions"}],"predecessor-version":[{"id":2125,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1927\/revisions\/2125"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/2087"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=1927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=1927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=1927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}