Changing the Color of any Node in a Node List

Hi FreeCodeCamp!

I am a beginner coder working on some DOM manipulation in JavaScript. The assignment is to dynamically create a table when clicking the ‘Submit’ button, and to be able to click any square in the table and change the color to the color of the color selector element.

So far, the table creation is working, and the color selection is not. No matter what, the color changes only work before I create the table.

I also have a click listener on the h1 element, which confusingly does change to the correct color.

// Declare variables for the two number elements, and the submit button

var submit = document.getElementById('submit');
var inputWidth = document.getElementById('inputWidth');
var inputHeight = document.getElementById('inputHeight');

// Add a click listener to the Submit button that removes the existing table, and makes a new one based on the input height and with.

submit.addEventListener('click', function () {

    var table = document.querySelector('table');

    document.body.removeChild(table);

    var table = document.createElement('table')

    document.body.appendChild(table);

    table.setAttribute('id', "table");
   

    for (i = 0; i < inputHeight.value; i++) {
        
        var tr = document.createElement('tr');
        table.appendChild(tr);

        for (j = 0; j < inputWidth.value; j++) {
            var td = document.createElement('td');
            tr.appendChild(td);    
        }
    }

    tds = document.querySelectorAll('td');
    console.log(tds);

});

// Attempt to change color of a clicked 'td' element after the table has been created (Not working)

console.log(document.querySelector('body'));

var tds = document.querySelectorAll('td');
console.log(tds)
console.log(document.querySelector('body'));

for (const td of tds) {
    var colorSelectorColor = colorSelector.value;
    console.log(td);
    td.addEventListener('click', function () {
        td.style.backgroundColor = colorSelectorColor;
    });
}

// ----Test Color Selector on H1 Header---- Works Better // 

var h1 = document.getElementById("h1");

var table = document.getElementById("table");

var colorSelectorColor = document.getElementById("colorSelector").value;

console.log(colorSelectorColor);

h1.addEventListener('click', function () {

    var colorSelector = document.getElementById("colorSelector");

    h1.style.color = colorSelector.value;

    console.log(h1.style.color.value);

});
1 Like

This is the HTML and CSS

<!DOCTYPE html>

<html>

<head>
    
    <title>Table Cell Color Clicker</title>

    <link rel="stylesheet" href="styles.css">

</head>

<body>
    
<h1 id="h1">Pick a Color, Click square on Table</h1>

<hr>

<input id=colorSelector type="color">

<br>

<hr>

<br>


<span> Width </span>
<input type="number" id="inputWidth" value="2">  
<span> Height </span>
<input label="height" type="number" id="inputHeight" value = "2">
<button type="submit" id="submit">Submit</button>

<br>

<table id="table" style="border: 1px solid black">
 <tr>
     <td></td>
     <td></td>
 </tr>
 <tr>
     <td></td>
     <td></td>
 </tr>
</table>

<br>

<br>

<!-- <p>This is Para 1</p>

<p>This is Para 2</p> -->

<script src="script.js"></script>

</body>

</html>

This is the CSS

/* tr {
    border: solid 1px black;
} */


td {
    border: solid 1px black;
    height: 50px;
    width: 50px;
    
}

#h1 {
    color: darkblue;
}

p {
    border: 1px solid blue;
}

/* table {
    margin: auto;
} */

Do you mind putting this up on codepen or something so you can share a working demo?

Sure. It will take me til tomorrow at lunch.

Here is a link to the project on codepen

What do you get if you log colorSelector or colorSelectorColor in this bit? I’d test it myself, but I’m on my phone.

Compare this colorSelectorColor assignment, versus the one for the h1 that works.

Console.log(colorSelector) gives the html of the input tag in which the color selector resides.

Console.log(colorSelectorColor) logs #000000 when the page loads, and does not change when I change the color.

I must remind you, the h1 element changes to the current, not original, color of the input element on click.

I didn’t read your code yet. But I think the problem is that you don’t add event listeners for your newly created tables. When you remove your ‘table’ from the code, the event listeners are gone too.

Sorry. I was doing a different thing and misunderstood your problem.


Your problem is the fact that you’re defining the color picker value outside of your event listener function.

But well. You’ll need to solve the other thing too, so, when you try if you can’t make It work I’ll help you again.

1 Like

This was it. I moved the variable colorSelectorColor inside the eventListener - need to review variable scope it appears :slight_smile:

I also wrapped the color but in a function, and called the function in the submit button click event as well.

I’ll update the codepen tmrw if you want to check it out.
Thank you!

1 Like

You’re welcome! If you need more help with anything, you can say

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.