<?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: jabrij93</title>
    <description>The latest articles on DEV Community by jabrij93 (@jabrij93).</description>
    <link>https://dev.to/jabrij93</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%2F899480%2Fbdae053d-cdb9-4603-be63-a112566d6372.png</url>
      <title>DEV Community: jabrij93</title>
      <link>https://dev.to/jabrij93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jabrij93"/>
    <language>en</language>
    <item>
      <title>Buttons not working, xxxx.addEventListener is not a function. Please help !</title>
      <dc:creator>jabrij93</dc:creator>
      <pubDate>Thu, 28 Jul 2022 11:08:26 +0000</pubDate>
      <link>https://dev.to/jabrij93/buttons-not-working-xxxxaddeventlistener-is-not-a-function-please-help--427d</link>
      <guid>https://dev.to/jabrij93/buttons-not-working-xxxxaddeventlistener-is-not-a-function-please-help--427d</guid>
      <description>&lt;p&gt;I created a 16 x 16 grid where I can etch a sketch on that grid. It's working with the default colour that I use. When I try adding buttons to change colours to sketch. I can't seem to make it work. I've tried various methods and writing it with various ways but again and again I failed. I want it when I click on the buttons, it changes colour when I sketch on it. I'll include below the previous code that it's working and one of the ways that I tried to make it work. Can you please have a look on my code ?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let container = document.querySelector('.container');
let rows = document.getElementsByClassName('gridRow');
let columns = document.getElementsByClassName('gridColumn');
const blue = document.getElementsByClassName('blue');
const eraser = document.getElementsByClassName('eraser');
const black = document.getElementsByClassName('black');
let reset = document.getElementById('reset');

function createGrid(number) {
    makeRow(number);
    makeColumn(number);
    changeColours();
}

function makeRow(numberOfRow) {
    for (let i = 0; i &amp;lt;numberOfRow; i++) {
        let row = document.createElement('div');
        container.appendChild(row);
        row.classList.add('gridRow');
    }
}

function makeColumn(numberOfColumn, selection) {
    for ( let i = 0; i &amp;lt; rows.length; i++) {
        for ( let j = 0; j &amp;lt; numberOfColumn; j++) {
            let column = document.createElement('div');   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;------- The part below is what I tried, erase it if you want it to work with just one colour -------&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    if (selection == 'blue') {
        column.addEventListener('mouseenter', function() {
            column.classList.add('blue');
        }) 
        } else if (selection == 'eraser') {
            column.addEventListener('mouseenter', function()  {
                column.classList.add('eraser');
        })
        } else if (selection == 'black') {
            column.addEventListener('mouseenter', function()  {
                column.classList.add('black');
        })
        } else {
            column.addEventListener('mouseenter', function()  {
                column.classList.add('colored'); 
            })
        }    
            //  column.addEventListener('mouseleave', () =&amp;gt; {
            //     column.classList.remove('colored');
            //  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;------- Just erase part of the code above if you want to make it work -------&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            rows[j].appendChild(column);
            column.classList.add('gridColumn');
        }   
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;------- The part below is what I tried, erase it if you want it to work with just one colour -------&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blue.addEventListener('click', function() {
        makeColumn(number, 'blue');
    }) 

eraser.addEventListener('click', function() {
        makeColumn(number, 'white');
    })

black.addEventListener('click', function() {
        makeColumn(number, 'black');
     })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;------- Just erase part of the code above if you want to make it work -------&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createGrid(16);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;@importurl('&lt;a href="https://fonts.googleapis.com/css2family=Asap:wght@400;600;700&amp;amp;display=swap'"&gt;https://fonts.googleapis.com/css2family=Asap:wght@400;600;700&amp;amp;display=swap'&lt;/a&gt;);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
    display: flex;
    height: 100%;
    width: 100%;
    flex-direction: column;
    background-color: beige;
    font-family: Asap, sans-serif;
    margin: 0;
    padding: 0;
    justify-content: center;
    align-content: center;
    align-items: center;
}

.header {
    display: flex;
    flex: 1;
    justify-content: center;

}

#setGridSize {
    display: inline-flex;
    justify-content: center;
    flex: 1;
    gap: 12px;
}

#guide {
    text-align: center;
    margin: 1px;
    font-family:  Asap, sans-serif;
    color: red;
    font-size: 13px;;
}

.container {
    display: flex;
    justify-content: center;
    align-content: center;
    align-items: center;
    border: 1px solid black;
    width: 550px;
    height: 550px;
}

.gridColumn {
    display: inline-flex;
    border: 1px solid beige;
    margin: -1px 0;
    width: 30px;
    height: 30px;
}

.colored{
    background: red;
  }

  .buttons {
    display: flex;
    flex: 1;
    gap: 20px;
    margin: 10px;
  }

  .blue {
    background: blue;
  }

  .eraser {
    background: white;
  }

  .black {
    background: black;
  }


&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;DOM Manipulation and Events&amp;lt;/title&amp;gt;
    &amp;lt;script src="javascript.js" defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;link rel="stylesheet" href="styles.css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h1 class="header"&amp;gt; Let's sketch ! &amp;lt;/h1&amp;gt;
    &amp;lt;div id="setGridSize"&amp;gt;
        &amp;lt;p&amp;gt; Grid size &amp;lt;/p&amp;gt; &amp;lt;input type="text" placeholder="Size of Board" class="size-box"&amp;gt; 
        &amp;lt;button id="submit" &amp;gt; Submit &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;p id="guide"&amp;gt; Enter a number between 2 to 99&amp;lt;/p&amp;gt;

      &amp;lt;div class="container"&amp;gt;&amp;lt;/div&amp;gt;

     &amp;lt;div class="buttons"&amp;gt;
        &amp;lt;button class="blue"&amp;gt; Blue &amp;lt;/button&amp;gt;
        &amp;lt;button class="eraser" &amp;gt; Eraser &amp;lt;/button&amp;gt;
        &amp;lt;button class="black"&amp;gt; Black &amp;lt;/button&amp;gt;
        &amp;lt;button class="rainbow" &amp;gt; Rainbow &amp;lt;/button&amp;gt;
        &amp;lt;button class="reset" &amp;gt; Reset&amp;lt;/button&amp;gt;
     &amp;lt;/div&amp;gt; 
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
