-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesigns.js
48 lines (42 loc) · 1.33 KB
/
designs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//the table var
var mainTable = document.querySelector('#pixelCanvas');
//the color var
var color = document.getElementById("colorPicker");
//the height var
var heightEl = document.getElementById("inputHeight");
//the width var
var widthEl = document.getElementById("inputWidth");
// When size is submitted by the user, call makeGrid() or remove Existing grid
document.querySelector('#sizePicker').addEventListener('submit', function (evt){
evt.preventDefault();
//clearing the grid
if (mainTable.hasChildNodes()){
while (mainTable.lastElementChild) {
mainTable.removeChild(mainTable.lastElementChild);
}
// Select size input and creat Grid
var height = heightEl.value;
var width = widthEl.value;
makeGrid(height,width);
}else {
// Select size input and creat Grid
var height = heightEl.value;
var width = widthEl.value;
makeGrid(height,width);
}
});
//addEventListener to color eatch cell
mainTable.addEventListener('click',function(evt){
// Select color input
evt.target.style.backgroundColor = color.value;
})
//afunction that makes a Grid
function makeGrid(height,width) {
for (let i = 0 ; i < height ; i++){
const tableRow = document.createElement('tr');
for (let x = 0; x < width ; x++){
tableRow.appendChild(document.createElement('td'));
}
mainTable.appendChild(tableRow);
}
}