<?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: Brian Ndickers</title>
    <description>The latest articles on DEV Community by Brian Ndickers (@ndickers).</description>
    <link>https://dev.to/ndickers</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%2F1138449%2Ff6943799-e941-4c79-99fc-bf0a89b85b0c.png</url>
      <title>DEV Community: Brian Ndickers</title>
      <link>https://dev.to/ndickers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ndickers"/>
    <language>en</language>
    <item>
      <title>Building shopping cart with html, tailwindcss and Javascript</title>
      <dc:creator>Brian Ndickers</dc:creator>
      <pubDate>Sat, 16 Sep 2023 18:04:34 +0000</pubDate>
      <link>https://dev.to/ndickers/building-shopping-cart-with-htmltailwindcss-and-javascript-1i5p</link>
      <guid>https://dev.to/ndickers/building-shopping-cart-with-htmltailwindcss-and-javascript-1i5p</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;br&gt;
A shopping cart is a compulsory section in modern ecommerce web application. Ecommerce web apps processes payment of products. Through shopping cart, the web app is able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep track of items selected.&lt;/li&gt;
&lt;li&gt;Get price of each selected item.&lt;/li&gt;
&lt;li&gt;Get quantity of items to be purchased.&lt;/li&gt;
&lt;li&gt;Compute shipping fees&lt;/li&gt;
&lt;li&gt;Compute total amount to be paid for the items.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, shopping cart facilitates the purchase of product or service.&lt;/p&gt;

&lt;p&gt;In this tutorial, I'm going to illustrate how to build an interactive shopping cart with html, tailwindcss and javascript.&lt;/p&gt;

&lt;p&gt;To follow along with this tutorial, you need to be familiar with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;html&lt;/li&gt;
&lt;li&gt;tailwindcss&lt;/li&gt;
&lt;li&gt;javascript&lt;/li&gt;
&lt;li&gt;dom manipulation&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This tutorial assumes you already select items from the home page. Now you are in the shopping cart page.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's start creating shopping cart.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create html file and configure tailwindcss
&lt;/h2&gt;

&lt;p&gt;First create &lt;code&gt;index.html&lt;/code&gt; file and configure tailwindcss to &lt;code&gt;index.html&lt;/code&gt;. For installation of tailwindcss in your project, checkout &lt;a href="https://tailwindcss.com/docs/installation" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
Now create &lt;code&gt;index.html&lt;/code&gt; in our project.&lt;/p&gt;

&lt;p&gt;Add the &lt;code&gt;head&lt;/code&gt; tag. Inside &lt;code&gt;head&lt;/code&gt; tag, link the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;google font to use in the project (&lt;em&gt;fraunces, montserrat&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;output.css&lt;/code&gt; file (&lt;em&gt;generated by tailwindcss when project executes&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt; file (&lt;em&gt;created later&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is how &lt;code&gt;head&lt;/code&gt; tag looks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.gstatic.com"&lt;/span&gt; &lt;span class="na"&gt;crossorigin&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
    &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=DM+Sans:opsz,wght@9..40,400;9..40,700&amp;amp;family=Fraunces:opsz,wght@9..144,500;9..144,700&amp;amp;family=Montserrat:wght@400;500;700&amp;amp;display=swap"&lt;/span&gt;
    &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"./output.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./index.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;cart&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to the &lt;code&gt;body&lt;/code&gt; tag. The &lt;code&gt;body&lt;/code&gt; tag contains, the &lt;code&gt;header&lt;/code&gt; tag, &lt;code&gt;section&lt;/code&gt; tag and &lt;code&gt;footer&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;header&lt;/code&gt; tag consist of the cart logo. The &lt;code&gt;section&lt;/code&gt; tag is currently empty (&lt;em&gt;later add cart items&lt;/em&gt;). The &lt;code&gt;footer&lt;/code&gt; tag contains cart total price, shipping fee and checkout price and checkout &lt;code&gt;button&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;main&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"md:p-6 md:pt-10 px-2 pt-6"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"md:flex md:flex-row md:items-center md:justify-between"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;" flex md:flex flex-row gap-x-2 pb-3 items-center"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./images/bag-2.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"text-darkBlue text-2xl font-bold"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Cart&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lg:flex lg:flex-row lg:justify-start"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lg:w-[65%] lg:pr-4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;
           &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"md:w-full border-y-2 py-4 border-lineColor flex flex-row justify-between text-grey font-medium"&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"md:flex md:w-[45%] md:flex-row md:justify-between "&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;PRODUCT&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;PRICE&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"md:flex md:w-[30%] md:flex-row md:justify-between"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;TOTAL&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;QTY&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"add-items"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"add-cart-items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="c"&gt;&amp;lt;!-- purchased items --&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;footer&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"bg-totalsGrey p-4 lg:px-4 lg:p-0 lg:w-[35%]"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-section"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;TOTAL AMOUNT IN CART&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"add-items-cost"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;$&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-section"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;SHIPPING FEE&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"shipping-fee"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;$&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-section"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;TOTAL AMOUNT&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
             &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"total-amount-to-pay"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;$&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'checkout-btn'&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"total-amount mt-4 py-3 w-full active:bg-grey hover:bg-darkBlue bg-blue text-white "&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Checkout &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"checkout-btn-amount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;708.85&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;/footer&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The colors used in this project are custom colors (&lt;em&gt;not tailwind default colors&lt;/em&gt;). These colors are &lt;code&gt;darkBlue&lt;/code&gt;, &lt;code&gt;totalsGrey&lt;/code&gt;, &lt;code&gt;grey&lt;/code&gt;. For more information about tailwind custom colors check &lt;a href="https://tailwindcss.com/docs/customizing-colors" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Create two javascript files
&lt;/h2&gt;

&lt;p&gt;The first file is &lt;code&gt;item.js&lt;/code&gt; file. This file has &lt;code&gt;newItem&lt;/code&gt; function. The &lt;code&gt;newItem&lt;/code&gt; function takes an object as a parameter. The object has three properties(&lt;code&gt;image&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The function has two local variables named &lt;code&gt;createDiv&lt;/code&gt; and &lt;code&gt;itemHtml&lt;/code&gt;. Variable &lt;code&gt;createDiv&lt;/code&gt; stores newly created html div. The &lt;code&gt;itemHtml&lt;/code&gt; stores html code in a string variable.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;innerHtml&lt;/code&gt; property of &lt;code&gt;createDiv&lt;/code&gt; is assign to &lt;code&gt;itemHtml&lt;/code&gt;. The &lt;code&gt;newItem&lt;/code&gt; function returns &lt;code&gt;createDiv&lt;/code&gt;. The function is then exported.&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;newItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;createDiv&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&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;itemHtml&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;div class="border-b-2 md:justify-between border-lineColor flex flex-row py-6"&amp;gt;
    &amp;lt;div class="flex md:w-[45%] md:justify-between flex-col md:flex-row md:items-center w-[60%] "&amp;gt;
      &amp;lt;div class="flex flex-row gap-x-2"&amp;gt;
        &amp;lt;img class="max-w-[7.5rem] h-auto" src="&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" alt="" srcset="" /&amp;gt;
        &amp;lt;div class="md:pr-6 md:flex md:flex-col md:justify-center" &amp;gt;
          &amp;lt;p class="text-lightGrey text-sm md:text-base"&amp;gt;Footwear&amp;lt;/p&amp;gt;
          &amp;lt;p  style="white-space:wrap;overflow:hidden;width:100%" class="item-description md:text-lg text-base"&amp;gt;
            &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
          &amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;p class="self-end md:self-center text-xl md:text-2xl font-bold mr-4 md:mr-0"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="w-[40%] md:w-[30%] flex md:flex-row-reverse md:items-center flex-col items-end justify-between"&amp;gt;
      &amp;lt;p id="total-item-cost" class="text-xl md:text-2xl text-purple font-bold"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/p&amp;gt;
      &amp;lt;div
        class="flex flex-row gap-x-2 items-center border-y-[2px] border-lineColor"
      &amp;gt;
        &amp;lt;button id="decrease" class="increment-btn"&amp;gt;-&amp;lt;/button&amp;gt;
        &amp;lt;p class="px-2"&amp;gt;1&amp;lt;/p&amp;gt;
        &amp;lt;button id="increase" class="increment-btn "&amp;gt;+&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;createDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;itemHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;createDiv&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;p&gt;Create &lt;code&gt;index.js&lt;/code&gt; file. In &lt;code&gt;index.js&lt;/code&gt; file import the &lt;code&gt;newItem&lt;/code&gt; function from &lt;code&gt;item.js&lt;/code&gt; file. Rename the imported function to &lt;code&gt;newShoeItem&lt;/code&gt;.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;newShoeItem&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./item.js&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;p&gt;The &lt;code&gt;index.js&lt;/code&gt; file, contains an array of objects named &lt;code&gt;shoes&lt;/code&gt;. The object has &lt;code&gt;image&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;price&lt;/code&gt; properties. The objects displays later as cart selected items.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nike Air Force 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/e777c881-5b62-4250-92a6-362967f54cca/air-force-1-07-womens-shoes-b19lqD.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Air Jordan 4 'Black Cat'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;240&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://images.stockx.com/images/Air-Jordan-4-Retro-Black-Cat-2020-Product.jpg?fit=fill&amp;amp;bg=FFFFFF&amp;amp;w=700&amp;amp;h=500&amp;amp;fm=webp&amp;amp;auto=compress&amp;amp;q=90&amp;amp;dpr=2&amp;amp;trim=color&amp;amp;updated_at=1606315877&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Air Jordan 7 Retro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://static.nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/bd3da02f-2ddc-4f0c-bb7b-f94c634cc0dd/air-jordan-7-retro-mens-shoes-7Zr804.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Air Jordan 1 Utility&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;170&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/956b0723-ef2f-4b49-8982-e2ec37e14f9c/air-jordan-1-utility-mens-shoes.png&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;p&gt;Add &lt;code&gt;DOMContentLoaded&lt;/code&gt; event listener. The event calls &lt;code&gt;addItemToCart&lt;/code&gt; function after loading dom content.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;addItemsToCart&lt;/code&gt; function does the follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Get empty &lt;code&gt;select&lt;/code&gt; tag with id &lt;code&gt;add-items&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a loop to get each item object in the array of shoes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call &lt;code&gt;newShoeItem&lt;/code&gt;(&lt;em&gt;imported function&lt;/em&gt;) passing each shoe as argument&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Insert returned html code to empty &lt;code&gt;select&lt;/code&gt; tag of id &lt;code&gt;add-items&lt;/code&gt;&lt;br&gt;
&lt;/p&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="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;addItemToCart&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addItemToCart&lt;/span&gt;&lt;span class="p"&gt;()&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;addSelectedShoes&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;add-items&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;shoes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shoe&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;addSelectedShoes&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="nf"&gt;shoeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shoe&lt;/span&gt;&lt;span class="p"&gt;));&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;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now run &lt;code&gt;index.html&lt;/code&gt; file in the browser. Your output should resemble the image below&lt;/p&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Finitial-cart.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Finitial-cart.png" alt="Initial stage of shopping cart" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Quantity manipulation
&lt;/h2&gt;

&lt;p&gt;You'll notice the increment(&lt;code&gt;+&lt;/code&gt;) and decrement(&lt;code&gt;-&lt;/code&gt;) buttons are not working when clicked. You should make the buttons functional in &lt;code&gt;index.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Increment quantity
&lt;/h3&gt;

&lt;p&gt;Start with increment button.To make items quantity increase when &lt;code&gt;+&lt;/code&gt; button is clicked, follow the steps below:&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;addItemsToCart&lt;/code&gt; function&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Get all increment buttons&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementButtons&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;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#increase&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;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;forEach&lt;/code&gt; method to trigger &lt;code&gt;increaseItems&lt;/code&gt; function on each button.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;incrementButtons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;increaseItems&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;increaseItems&lt;/code&gt; function, does the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pass in &lt;code&gt;button&lt;/code&gt; as parameter&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;click&lt;/code&gt; event listener to the &lt;code&gt;button&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;event&lt;/code&gt; object to get the current quantity of the items&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Increments the current quantity&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;increaseItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previousElementSibling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&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;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Decrement quantity
&lt;/h3&gt;

&lt;p&gt;Make items quantity decrease when clicking &lt;code&gt;-&lt;/code&gt; button.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;addItemsToCart&lt;/code&gt; function&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Get all decrement buttons&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrementButtons&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;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#decrease&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;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;forEach&lt;/code&gt; method to trigger &lt;code&gt;decreaseItems&lt;/code&gt; function on each button.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;decrementButtons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decreaseItems&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;decreaseItems&lt;/code&gt; function does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pass in &lt;code&gt;button&lt;/code&gt; as parameter&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;click&lt;/code&gt; event listener to the &lt;code&gt;button&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;event&lt;/code&gt; object to get current item quantity&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Decrement current quantity, if current quantity is greater than 1&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;decreaseItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentQuantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;textContent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentQuantity&lt;/span&gt; &lt;span class="o"&gt;&amp;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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;textContent&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&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;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Now, you are able to increment and decrement the quantity when &lt;code&gt;+&lt;/code&gt; button and &lt;code&gt;-&lt;/code&gt; button are clicked as shown below.&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fincrement-cart.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fincrement-cart.png" alt="increment and decrement items quantity in shopping cart" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
You are able to increase and decrease the quantity. But the quantity amount is still constant.&lt;/p&gt;
&lt;h2&gt;
  
  
  Updating total item price
&lt;/h2&gt;

&lt;p&gt;Multiply item price with quantity to get total item price&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;totalItemPrice&lt;/code&gt; function. The function will take in two parameters &lt;code&gt;element&lt;/code&gt; and &lt;code&gt;siblingPostion&lt;/code&gt;. The &lt;code&gt;totalItemPrice&lt;/code&gt; function does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;element&lt;/code&gt; parameter(&lt;em&gt;clicked button&lt;/em&gt;) to get item price by &lt;a href="https://www.easeout.co/blog/2020-12-28-traversing-the-dom/" rel="noopener noreferrer"&gt;dom traverse&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;siblingPosition&lt;/code&gt; parameter to get item quantity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiply item price with item quantity to get total item price&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;element&lt;/code&gt; to get text content of total item price and update total item price&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;totalItemPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;siblingPosition&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;itemPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previousElementSibling&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;textContent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;itemQuantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;siblingPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalItemPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;itemPrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;itemQuantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previousElementSibling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`$&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;totalItemPrice&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Now you need to call &lt;code&gt;totalItemPrice&lt;/code&gt; function inside &lt;code&gt;decreaseItem&lt;/code&gt; and &lt;code&gt;increaseItem&lt;/code&gt; function. This will make item total price update everytime increment and decrement button is clicked.&lt;/p&gt;

&lt;p&gt;Call &lt;code&gt;totalItemPrice&lt;/code&gt; function inside click event function of &lt;code&gt;increaseItem&lt;/code&gt; and &lt;code&gt;decreaseItem&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Use the &lt;code&gt;event&lt;/code&gt; object to get the arguments in &lt;code&gt;totalItemPrice&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;increaseItem&lt;/code&gt; the arguments used in event function are &lt;code&gt;e.target&lt;/code&gt; and &lt;code&gt;e.target.previousElementSibling&lt;/code&gt;.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;previousSibling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previusElementSibling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;totalItemPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;previousSibling&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;decreaseItem&lt;/code&gt; the arguments used in event function are &lt;code&gt;e.target&lt;/code&gt; and &lt;code&gt;e.target.nextElementSibling&lt;/code&gt;.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nextSibling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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="nf"&gt;totalItemPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;previousSibling&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;e.target&lt;/code&gt; is the button clicked. The &lt;code&gt;e.target.previousElementSibling&lt;/code&gt; and &lt;code&gt;e.target.nextElementSibling&lt;/code&gt; gets item quantity&lt;/p&gt;

&lt;p&gt;Total item price updates when &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; buttons are clicked.&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Ftotal-item-price.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Ftotal-item-price.png" alt="update item total price when increase or decrease button is pressed" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
Now sum up item total price.&lt;/p&gt;
&lt;h3&gt;
  
  
  Compute cart Total price, shipping fee and checkout price
&lt;/h3&gt;

&lt;p&gt;A cart need to sum up total item price, compute  shipping fees and calculate the checkout price of the items. You are going to create &lt;code&gt;calculateTotals&lt;/code&gt; function. The function returns an array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;calculateTotals&lt;/code&gt; function does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare &lt;code&gt;cartTotalPrice&lt;/code&gt; variable and initialize &lt;code&gt;cartTotalPrice&lt;/code&gt; to 0.&lt;/li&gt;
&lt;li&gt;Get each item total price and add them to &lt;code&gt;cartTotalPrice&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Declare &lt;code&gt;shippingFee&lt;/code&gt; variable and initialize it to 7% of &lt;code&gt;cartTotalPrice&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Declare &lt;code&gt;totalCashToPay&lt;/code&gt; variable and initialize it to sum of &lt;code&gt;cartTotalPrice&lt;/code&gt; and &lt;code&gt;shippingFee&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Returns an array consisting of three variables; &lt;code&gt;cartTotalPrice&lt;/code&gt;, &lt;code&gt;shippingFee&lt;/code&gt; and &lt;code&gt;totalCashToPay&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateTotals&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cartTotalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;getItemPrices&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;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#total-item-cost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;getItemPrices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;itemPrice&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;cartTotalPrice&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;itemPrice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="p"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shippingFee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTotalPrice&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;100&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;totalCashToPay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cartTotalPrice&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shippingFee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cartTotalPrice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&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;shippingFee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&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;totalCashToPay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Updating cart Total price, shipping fee and checkout price on corresponding elements
&lt;/h3&gt;

&lt;p&gt;You need to update the UI with computed values. Everytime the user decrement or increment quantity, the UI needs to show real time values. &lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;updateTotals&lt;/code&gt; function which takes in two parameters. The parameters are &lt;code&gt;elementId&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt;. The function gets the element of the id and update the element text content with the &lt;code&gt;value&lt;/code&gt;.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateTotals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elementId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;value&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;element&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="nx"&gt;elementId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&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;p&gt;Call &lt;code&gt;calculateTotals&lt;/code&gt; and &lt;code&gt;updateTotals&lt;/code&gt; function inside the &lt;code&gt;totalItemPrice&lt;/code&gt; function. This will make updates when either increment or decrement button is clicked. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;calculateTotals&lt;/code&gt; function will return an array. Then &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="noopener noreferrer"&gt;destructure&lt;/a&gt; the array to get three independent value.&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;shippingFee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;checkoutPrice&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculateTotals&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now call the &lt;code&gt;updateTotals&lt;/code&gt; function to change the initial value of the elements to current value.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update content of element with id &lt;code&gt;add-items-cost&lt;/code&gt; to &lt;code&gt;totalPrice&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;Update content of element with id &lt;code&gt;shipping-fee&lt;/code&gt; to &lt;code&gt;shippingFee&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;Update content of element with id &lt;code&gt;total-amount-to-pay&lt;/code&gt; to &lt;code&gt;checkoutPrice&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update checkout button to read &lt;code&gt;checkoutPrice&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;updateTotals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#add-items-cost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;`$&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;updateTotals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#shipping-fee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;`$&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;shippingFee&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;updateTotals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#total-amount-to-pay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;`$&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;checkoutPrice&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;updateTotals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#checkout-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;`Checkout $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;checkoutPrice&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally your shopping cat should be responsive and resemble the image below.&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fcheckout-cart.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fcheckout-cart.png" alt="Final shopping cart image " width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
Now the shopping cart is responsive and manipulable. In summary the shopping cart is able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increase and decrease item quantity&lt;/li&gt;
&lt;li&gt;Compute item total price&lt;/li&gt;
&lt;li&gt;Sum item total price&lt;/li&gt;
&lt;li&gt;Compute shipping fee&lt;/li&gt;
&lt;li&gt;Compute total checkout price&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Drag and Drop implementation using html/css and Javascript</title>
      <dc:creator>Brian Ndickers</dc:creator>
      <pubDate>Sun, 03 Sep 2023 21:12:57 +0000</pubDate>
      <link>https://dev.to/ndickers/drag-drop-implementation-using-htmlcss-and-javascript-8mc</link>
      <guid>https://dev.to/ndickers/drag-drop-implementation-using-htmlcss-and-javascript-8mc</guid>
      <description>&lt;p&gt;&lt;strong&gt;Drag &amp;amp; drop&lt;/strong&gt; is among the important technique a web developer must be comfortable working with. In this article we are going to cover important events involved in &lt;strong&gt;drag &amp;amp; drop&lt;/strong&gt;.  For better understanding of this article, you need to be familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;html&lt;/li&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;DOM events. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Drag &amp;amp; drop&lt;/strong&gt; events can be categorised into two:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Events that happens on &lt;code&gt;dragged element&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Events that happens on &lt;code&gt;target element&lt;/code&gt;(&lt;strong&gt;dropZone&lt;/strong&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For these events to work, set draggable attribute in dragged element to true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;draggable=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"dragged-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This is our draggable element&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;  Images and links are draggable by default (No need to set draggable attribute to true)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1 Events that happen on dragged element
&lt;/h2&gt;

&lt;p&gt;These are events that trigger when draggable elements moves from their original position to final position. The events are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dragstart event&lt;/li&gt;
&lt;li&gt;drag event&lt;/li&gt;
&lt;li&gt;dragend event&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's discuss each event to see when and how to use these events.&lt;/p&gt;

&lt;h3&gt;
  
  
  a) Dragstart event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dragstart&lt;/code&gt; event triggers when user start to move &lt;code&gt;dragged&lt;/code&gt; element.This event uses &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer"&gt;&lt;code&gt;dataTransfer&lt;/code&gt;&lt;/a&gt; object to set data. &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer"&gt;&lt;code&gt;dataTransfer&lt;/code&gt;&lt;/a&gt; object sets data using &lt;code&gt;setData&lt;/code&gt; method. The &lt;code&gt;setData&lt;/code&gt; method takes in two parameter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;format&lt;/strong&gt; of data to be set (&lt;code&gt;"text/plain"&lt;/code&gt;, &lt;code&gt;"text/html"&lt;/code&gt; etc)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;data&lt;/strong&gt; value (anything in string form)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataTransfer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The data set above, is later retrieved and used in &lt;code&gt;drop&lt;/code&gt; event.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  dragstart event implemention
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;dragstart&lt;/code&gt; event listener to dragged element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get &lt;code&gt;id&lt;/code&gt; of &lt;code&gt;dragged&lt;/code&gt; element for later use (use &lt;code&gt;event.target.getAttribute("id")&lt;/code&gt; to get id).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set data(in &lt;code&gt;text/plain&lt;/code&gt; format) to &lt;code&gt;id&lt;/code&gt; of dragged element. The data is set using&lt;code&gt;setData&lt;/code&gt; method in &lt;code&gt;dataTransfer&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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;draggedElement&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="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.dragged-item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;draggedElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dragstart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataTansfer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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;
  
  
  b) drag event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;drag&lt;/code&gt; event triggers when dragged element is moving. The &lt;code&gt;drag&lt;/code&gt; event fires continuously since it triggers when moving dragged element. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Drag&lt;/code&gt; event provides an opportunity to change appearance of &lt;code&gt;dragged&lt;/code&gt; element so that users can visualize their action.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;drag&lt;/code&gt; event, change the appearance of &lt;code&gt;dragged&lt;/code&gt; element using css.&lt;/p&gt;

&lt;h4&gt;
  
  
  drag event implementation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;drag&lt;/code&gt; event listener to dragged element&lt;/li&gt;
&lt;li&gt;Get &lt;code&gt;dragged&lt;/code&gt; element(&lt;code&gt;e.target&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Change the color  of dragged element
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;draggedElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;grey&lt;/span&gt;&lt;span class="dl"&gt;"&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;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can ommit &lt;code&gt;drag&lt;/code&gt; event if you not changing appearance of dragged element&lt;/p&gt;

&lt;p&gt;It is possible to use &lt;code&gt;setData&lt;/code&gt; method in &lt;code&gt;dataTransfer&lt;/code&gt; object to set data in this &lt;code&gt;drag&lt;/code&gt; event.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  c) dragend event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dragend&lt;/code&gt; event triggers when drag operation is completed. Drag operation can either be successful or unsuccessful.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;dragend&lt;/code&gt; event allows you to reset appearance of dragged element to initial appearance of dragged element.&lt;/p&gt;

&lt;h4&gt;
  
  
  dragend event implementation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;dragend&lt;/code&gt; event listener to dragged element&lt;/li&gt;
&lt;li&gt;Get dragged element(&lt;code&gt;e.target&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Change the color  of dragged element to its initial color
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;draggedElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;grey&lt;/span&gt;&lt;span class="dl"&gt;"&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; You can ommit &lt;code&gt;dragend&lt;/code&gt; event if you not  changing appearance of dragged element&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2 Events that happen on drop target element
&lt;/h2&gt;

&lt;p&gt;These are events that occur when &lt;code&gt;dragged&lt;/code&gt; element moves over the drop target element(&lt;strong&gt;dropZone&lt;/strong&gt;). The events modifies the &lt;strong&gt;dropZone&lt;/strong&gt; element. The events are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dragenter event&lt;/li&gt;
&lt;li&gt;dragover event&lt;/li&gt;
&lt;li&gt;drop event&lt;/li&gt;
&lt;li&gt;dragleave event&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;let's discuss the above events and how they should be implemented.&lt;/p&gt;

&lt;h3&gt;
  
  
  a) dragenter event
&lt;/h3&gt;

&lt;p&gt;The&lt;code&gt;dragenter&lt;/code&gt; event fires when dragged element enters the &lt;strong&gt;dropZone&lt;/strong&gt;. This &lt;code&gt;dragenter&lt;/code&gt; event provides a visual feedback that a &lt;strong&gt;dropZone&lt;/strong&gt; element accepts dragged element(drop target).&lt;/p&gt;

&lt;h4&gt;
  
  
  dragenter event implemention
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;strong&gt;dropZone&lt;/strong&gt; element in html
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"drop-zone"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Get &lt;strong&gt;dropZone&lt;/strong&gt; element using selector
&lt;/li&gt;
&lt;/ol&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;dropZone&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="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.drop-zone&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;ol&gt;
&lt;li&gt;Add &lt;code&gt;dragenter&lt;/code&gt; event listener to &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;e.target&lt;/code&gt;, modify &lt;strong&gt;dropZone&lt;/strong&gt; element(change bacground color).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;dropZone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dragenter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotted 4px black&lt;/span&gt;&lt;span class="dl"&gt;"&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The default behavior of &lt;code&gt;dragenter&lt;/code&gt; is to avoid dropping of dragged element. The &lt;code&gt;e.preventDefault()&lt;/code&gt; prevents the default behaviour of &lt;code&gt;dragenter&lt;/code&gt; event hence allowing dropping of dragged element in &lt;strong&gt;dropZone&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  b) dragover event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dragover&lt;/code&gt; event fires when dragged element is over the &lt;strong&gt;dropZone&lt;/strong&gt;. This &lt;code&gt;dragover&lt;/code&gt; event fires every milliseconds. &lt;/p&gt;

&lt;p&gt;The main purpose of &lt;code&gt;dragover&lt;/code&gt; event is to prevent default. Preventing default, prevents dragged element from going back to its original position when handling &lt;code&gt;drop&lt;/code&gt; event.&lt;/p&gt;

&lt;h4&gt;
  
  
  dragover event implemention
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Get the &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;dragover&lt;/code&gt; event listener to &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Prevent default of &lt;code&gt;dragover&lt;/code&gt; event
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;dropZone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dragover&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&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;
  
  
  c) drop event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;drop&lt;/code&gt; event fires when dragged element drops in &lt;strong&gt;dropZone&lt;/strong&gt;. The &lt;code&gt;drop&lt;/code&gt; event is where we get the data that was set in &lt;code&gt;dragstart&lt;/code&gt; event and use the data. The data is obtained from &lt;code&gt;dataTransfer&lt;/code&gt; object using &lt;code&gt;getData&lt;/code&gt; method.&lt;/p&gt;

&lt;h4&gt;
  
  
  drop event implemention
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Get &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;drop&lt;/code&gt; event listener to &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Prevent default of &lt;code&gt;drop&lt;/code&gt; event (opening link when dropped)&lt;/li&gt;
&lt;li&gt;Get the data that was set in &lt;code&gt;dragstart&lt;/code&gt; event and store it in a variable called &lt;strong&gt;draggedElementId&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&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;draggedElementId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataTransfer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/plain&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; &lt;strong&gt;draggedElementId&lt;/strong&gt; is a string (&lt;code&gt;id&lt;/code&gt; of dragged element)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get the dragged element using &lt;code&gt;document.getElementById&lt;/code&gt; and store it in another variable called &lt;strong&gt;draggedElement&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&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;draggedElement&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="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;draggedElementId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Get the &lt;strong&gt;dropZone&lt;/strong&gt; element(&lt;code&gt;e.target&lt;/code&gt;) and  append the &lt;strong&gt;draggedElement&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;dropZone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;drop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&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;draggedElementId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataTransfer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&gt;"&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;draggedElement&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="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;draggedElementId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;draggedElement&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;p&gt;The &lt;strong&gt;draggedElement&lt;/strong&gt; moves inside &lt;strong&gt;dropZone&lt;/strong&gt; as child element.&lt;/p&gt;

&lt;h3&gt;
  
  
  d) dragleave event
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dragleave&lt;/code&gt; event fires when dragged element leaves the &lt;strong&gt;dropZone&lt;/strong&gt; area. This &lt;code&gt;dragleave&lt;/code&gt; event can be used to modify dropZone element to its original state( remove visual feedback in &lt;code&gt;dragenter&lt;/code&gt; event).&lt;/p&gt;

&lt;h4&gt;
  
  
  dragleave event implementation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Get &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;dragleave&lt;/code&gt; event listener to &lt;strong&gt;dropZone&lt;/strong&gt; element&lt;/li&gt;
&lt;li&gt;Reset &lt;strong&gt;dropZone&lt;/strong&gt; element to its original state
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;dropZone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dragleave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;solid 4px black&lt;/span&gt;&lt;span class="dl"&gt;"&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In summary, mastering these &lt;strong&gt;drag &amp;amp; drop&lt;/strong&gt; events is essential for creating interactive and user-friendly web applications. They allow you to build features like drag-and-drop interfaces, file uploads, and more.&lt;/p&gt;

&lt;p&gt;By understanding and effectively implementing these events, you can create dynamic and user-friendly interfaces with &lt;strong&gt;drag &amp;amp; drop&lt;/strong&gt; functionality, enhancing the user experience and interactivity of websites.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>html</category>
    </item>
    <item>
      <title>Building a Dynamic Sticky Note App with HTML, CSS, Vanilla JavaScript, and API</title>
      <dc:creator>Brian Ndickers</dc:creator>
      <pubDate>Sat, 02 Sep 2023 19:34:16 +0000</pubDate>
      <link>https://dev.to/ndickers/building-a-dynamic-sticky-note-app-with-html-css-vanilla-javascript-and-api-bfp</link>
      <guid>https://dev.to/ndickers/building-a-dynamic-sticky-note-app-with-html-css-vanilla-javascript-and-api-bfp</guid>
      <description>&lt;p&gt;In this article, you are going to learn about how to build a sticky note app using html,css and javascript. You're going to store the notes in Api and get the notes from api for display in the screen.&lt;/p&gt;

&lt;p&gt;Here are four steps we will follow to build sticky note app:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up html structure and css&lt;/li&gt;
&lt;li&gt;Create form to capture note&lt;/li&gt;
&lt;li&gt;Post note to Api&lt;/li&gt;
&lt;li&gt;Get notes from Api&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To follow along with this tutorial, you'll need to be familiar  with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;html and css&lt;/li&gt;
&lt;li&gt; javascript and dom manupilation&lt;/li&gt;
&lt;li&gt;fetch api&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; In this tutorial you will use &lt;a href="https://github.com/mockapi-io/docs/wiki/Quick-start-guide" rel="noopener noreferrer"&gt;mock api&lt;/a&gt; to store notes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Set up html structure and css
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;index.html&lt;/code&gt; file and link it to &lt;code&gt;style.css&lt;/code&gt; file. Add the following html code to &lt;code&gt;index.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The follow html code, also link &lt;code&gt;index.js&lt;/code&gt; file&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;head&amp;gt;
    &amp;lt;!-- link style.css file --&amp;gt;
    &amp;lt;link rel="stylesheet" href="./style.css" /&amp;gt;
    &amp;lt;title&amp;gt;my notes&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
 &amp;lt;body&amp;gt;
    &amp;lt;main&amp;gt;
      &amp;lt;header&amp;gt;
        &amp;lt;h1&amp;gt;My Notes&amp;lt;/h1&amp;gt;
        &amp;lt;a id="display-form-input"
          class="Add-new-note-btn"&amp;gt;+ Add a new note&amp;lt;/a&amp;gt;
      &amp;lt;/header&amp;gt;
      &amp;lt;section id="note-section" class="main-article-section"&amp;gt;
        &amp;lt;div id="article-div" class="flex-section-div"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/section&amp;gt;
    &amp;lt;/main&amp;gt;
    &amp;lt;script src="./index.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a &lt;code&gt;style.css&lt;/code&gt; file. Add the code below to &lt;code&gt;style.css&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
  --head-color: hsla(0, 0%, 13%, 1);
  --dark-blue: hsla(228, 69%, 13%, 1);
  --blue: hsla(228, 69%, 20%, 1);

  --content-color: hsla(0, 0%, 13%, 1);
  --greyish: hsla(0, 2%, 44%, 1);
  --white: hsla(0, 0%, 100%, 1);
}
body {
  position: relative;
  inline-size: 100%;
  block-size: 100%;
  font-family: "DM Sans", sans-serif;
  background: var(--white);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.8rem;
}
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
main {
  inline-size: 90%;
}

.add-new-note-btn {
  background-color: var(--dark-blue);
  font-size: 16px;
  line-height: 16px;
  display: inline-block;
  inline-size: 15%;
  text-align: center;
  text-decoration: none;
  padding: 15px 24px;
  color: var(--white);
  border-radius: 25px;
  margin-block-end: 1.5rem;
}
.submit-note{
  padding: 15px 24px;
  background:var(--dark-blue);
  border-radius: 20px;
  color: var(--white);
  margin-block-start: 1rem;
  inline-size: 40%;
  margin-inline-start:60%;
  border:none;
  white-space: nowrap;
}
textarea{
  resize: none;
  padding: 0.7rem;
  display: block;
  inline-size: 310px;
  block-size: 112px;
  border-radius: 10px;
  background:var(--greyish);
  color: var(--white);
}
.note-div{
  inline-size: 160px;
  block-size: 120px;
  background: #FCF5D2;
  border-radius: 10px;
  margin: 1rem;
  padding: 0.4rem;
}
#note-section{
  display: flex;
  flex-wrap: wrap;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;index.html&lt;/code&gt; file in the browser. The code output should ressemble the image below.&lt;/p&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Finitial-site-look.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Finitial-site-look.png" alt="Sticky note app initial look"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Create form to capture note
&lt;/h2&gt;

&lt;p&gt;Now user needs somewhere to type their note. In this section you will implement display form logic. When user clicks &lt;code&gt;+Add new note&lt;/code&gt; button, a form displays. Users can then type note in the &lt;code&gt;textarea&lt;/code&gt;. The steps below implements display form logic.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create &lt;code&gt;index.js&lt;/code&gt; file. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add click event listener to &lt;code&gt;+Add a new note&lt;/code&gt; button. The code below handles the clicked event by calling &lt;code&gt;displayForm&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addNewNote = document.querySelector(".Add-new-note-btn");

addNewNote.addEventListener("click",function (event){
  event.preventDefault();
  displayForm()
})

function displayForm(){
  const form = document.createElement("form");
    form.setAttribute("id", "note-form");
    // create textarea
    const textArea = document.createElement("textarea");
    textArea.setAttribute("id", "note");
    textArea.setAttribute("placeholder","Add note");
    // create add note button
    const button = document.createElement("button");
    button.setAttribute("class", "submit-note btn-state");
    button.setAttribute("id", "add-note");
    button.textContent = "Add note";
    // add textarea and button element to form created
    form.append(textArea, button);
    document.body.appendChild(form);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;index.html&lt;/code&gt; file and click &lt;code&gt;Add new note&lt;/code&gt; button. The browser should resemble the image below.&lt;/li&gt;
&lt;/ol&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fadd-note-screen.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fadd-note-screen.png" alt="Display form to add new note"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Post note to Api
&lt;/h2&gt;

&lt;p&gt;User is able to type the note in our application. But, the text vanishes when submitted. You need to prevent the default behavior of form submission. Then get note content and store the note in Api. &lt;/p&gt;

&lt;p&gt;To store note in Api, follow the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add click event listener to &lt;code&gt;add note&lt;/code&gt; button&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;postNote&lt;/code&gt; function passing in &lt;code&gt;url&lt;/code&gt; as parameter
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const postNoteBtn = document.querySelector("#add-note");
  postNoteBtn.addEventListener("click", function (e) {
    e.preventDefault();
    postNote("https://64e507a7c555638029140f2a.mockapi.io/note");
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;   &lt;code&gt;add note&lt;/code&gt; button event listener, is added inside &lt;code&gt;displayForm&lt;/code&gt; function to prevent &lt;code&gt;TypeError&lt;/code&gt; messege(&lt;em&gt;postNoteBtn is null&lt;/em&gt;) since the form to type text, is not yet displayed.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;postNote&lt;/code&gt; function does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gets user typed value.&lt;/li&gt;
&lt;li&gt;Creates new object(&lt;strong&gt;noteObject&lt;/strong&gt;) with &lt;code&gt;content&lt;/code&gt; as key&lt;/li&gt;
&lt;li&gt;Assign typed value to
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const noteObject = {content:typedValueFromUser};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Post &lt;strong&gt;noteObject&lt;/strong&gt; to Api using &lt;code&gt;fetch&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Create new &lt;code&gt;div&lt;/code&gt; html element&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;textContent&lt;/code&gt; property of new &lt;code&gt;div&lt;/code&gt; to value obtained from &lt;code&gt;fetch&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Append created &lt;code&gt;div&lt;/code&gt; to a section element of id &lt;code&gt;note-section&lt;/code&gt; in &lt;code&gt;index.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Remove the form element
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function postNote(url) {
  const note = document.getElementById("note").value;

  let noteObject = { content: note };
  //   request object used in fetch api
  const requestObject = {
    method: "POST",
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify(noteObject),
  };

  fetch(url, requestObject).then((response) =&amp;gt; {
    if (response.statusText == "Created") {
      response.json().then((noteObj) =&amp;gt; {
        const div = document.createElement("div");
        div.setAttribute("class", "note-div");
        div.textContent = noteObj.content;
        document.querySelector("#note-section").appendChild(div);
        document.getElementById("note-form").remove();
      });
    }
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Get notes from Api
&lt;/h2&gt;

&lt;p&gt;You are able to post note to api. You are also able to display note in the browser. But when the browser loads, every note disappear. You need to get all notes displayed in the browser when browser loads.&lt;/p&gt;

&lt;p&gt;Add load event listener to window object. When browser loads, it triggers &lt;code&gt;fetchNote&lt;/code&gt; function. &lt;code&gt;fetchNote&lt;/code&gt; function does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gets all notes from Api in array form&lt;/li&gt;
&lt;li&gt;Create div for each note in the array&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;textContent&lt;/code&gt; for each div to each note in array&lt;/li&gt;
&lt;li&gt;Append each &lt;code&gt;div&lt;/code&gt; to a section element of id &lt;code&gt;note-section&lt;/code&gt; in &lt;code&gt;index.html&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("load", function fetchNote() {
  fetch("https://64e507a7c555638029140f2a.mockapi.io/note").then((response) =&amp;gt; {
    if (response.statusText == "OK") {
      response.json().then((notes) =&amp;gt; {
        notes.forEach((note) =&amp;gt; {
          const div = document.createElement("div");
          div.setAttribute("class", "note-div");
          div.textContent = note.content;
          document.querySelector("#note-section").appendChild(div);
        });
      });
    }
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now you have created your own sticky note application. The end result for the sticky note resembles the image below&lt;/p&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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fend-sticky.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%2Fbryondickers.github.io%2Ftechnical-w%2Fimages%2Fend-sticky.png" alt="sticky note end product"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, You've embarked on a journey to create a dynamic and engaging Sticky Note application using the fundamental web technologies of HTML, CSS, and Vanilla JavaScript, enhanced with API integration.Through this step-by-step tutorial, we've learned how to :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up html structure and css&lt;/li&gt;
&lt;li&gt;Create form to capture note&lt;/li&gt;
&lt;li&gt;Post note to Api&lt;/li&gt;
&lt;li&gt;Get notes from Api&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember there are endless possibilities for expansion. You can expand your sticky note  app to edit and delete posted note.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>api</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
