Learn JavaScript - DOM background Colors Project
Background Color Table Fun
This exercise will build an interactive table of colored cells that can be clicked to apply the cell background color to the page. In Addition several buttons to update the background colors of the table cells. The table will be dynamically generated using values for columns and rows. The application will also include 3 interactive buttons to update the page elements.
Button actions when clicked :
The table can be created using JavaScript and loops. Set the number of rows for the main loop, and within that loop nest a second loop that will create td elements to the number of columns needed. Using the rows and columns the JavaScript code can then generate a table.
Use document.createElement() to create new page elements.
Using the loops below you can create a table with JavaScript code.
const tbl = document.createElement('table');
for (let y = 0; y < rows; y++) {
const tr = document.createElement('tr');
for (let x = 0; x < cols; x++) {
const td = document.createElement('td');
tr.append(td);
}
tbl.append(tr);
}
JavaScript and DOM methods you can use and properties to update elements.
Coding Exercise below :
Source Code
const output = document.querySelector('div');
console.log(output);
output.innerHTML = '';
const tabValue = {
rows: 5,
cols: 9
}
const main = addTable(output, tabValue.rows, tabValue.cols);
const btn1 = btnMaker(output, 'Random', 'blue', 'white');
const btn2 = btnMaker(output, 'MoveDown', 'red', 'white');
const btn3 = btnMaker(output, 'MoveLeft', 'purple', 'white');
btn3.addEventListener('click', (e) => {
const tds = main.querySelectorAll('td');
for (let i = 0; i < tds.length; i++) {
console.log(tds.length, i);
const bgColor = (i + 1 == tds.length) ? ranColor() : tds[i + 1].style.backgroundColor;
console.log(bgColor);
tds[i].style.backgroundColor = bgColor;
}
})
btn1.addEventListener('click', (e) => {
const tds = main.querySelectorAll('td');
console.log(tds);
tds.forEach(ele => {
console.log(ele);
ele.style.backgroundColor = ranColor();
})
})
btn2.addEventListener('click', (e) => {
const tds = main.querySelectorAll('td');
let holder = [];
tds.forEach((ele, ind) => {
let temp = ind - tabValue.cols;
Recommended by LinkedIn
let tempColor = '';
//console.log(temp);
if (temp >= 0) {
const el = tds[temp];
tempColor = el.style.backgroundColor;
//ele.style.backgroundColor =
} else {
tempColor = ranColor();
}
holder.push(tempColor);
})
console.log(holder);
holder.forEach((val, ind) => {
tds[ind].style.backgroundColor = val;
})
})
function ranColor() {
return `#${Math.random().toString(16).slice(2,8)}`;
}
function btnMaker(parent, html, clr, fntColor) {
const btn = document.createElement('button');
btn.innerHTML = html;
btn.style.backgroundColor = clr;
btn.style.color = fntColor;
return parent.appendChild(btn);
}
function addTable(parent, rows, cols) {
const tbl = document.createElement('table');
tbl.style.border = '1px solid black';
const tblby = document.createElement('tbody');
tbl.append(tblby);
let counter = 0;
let wid = (100 / cols) * 100;
for (let y = 0; y < rows; y++) {
const tr = document.createElement('tr');
for (let x = 0; x < cols; x++) {
const td = document.createElement('td');
counter++;
td.textContent = `${counter}`;
td.style.width = `${wid}px`;
td.style.border = '1px solid #ddd';
td.style.textAlign = 'center';
tr.append(td);
td.addEventListener('click', () => {
document.body.style.backgroundColor = td.style.backgroundColor;
})
}
tblby.append(tr);
}
return parent.appendChild(tbl);
}