-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
57 lines (51 loc) · 1.71 KB
/
script2.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
49
50
51
52
53
54
55
56
57
function createGrid(size){
const table=document.querySelector(".table");
table.style.gridTemplateRows=`repeat(${size},1fr)`;
table.style.gridTemplateColumns=`repeat(${size},1fr)`;
let tableRow=[];
for(let i=0;i<size;i++){
let tableColumn=[];
for(let j=0;j<size;j++){
const divElement=document.createElement("div");
divElement.setAttribute("id",`${i}${j}`);
tableColumn.push(divElement);
divElement.classList.add("tableElement");
divElement.addEventListener("mousedown",changeColor);
table.appendChild(divElement);
}
tableRow.push(tableColumn);
}
return tableRow;
}
function changeColor(x){
x.target.classList.add("tableElementHover");
}
function resetGridSameSize(){
let size=document.querySelector(".table").childElementCount;
size=Math.sqrt(size);
for(let i=0;i<size;i++){
for(let j=0;j<size;j++){
let temp=document.getElementById(`${i}${j}`);
temp.parentNode.removeChild(temp);
}
}
createGrid(size);
}
function resetGrid(){
let size=document.querySelector(".table").childElementCount;
size=Math.sqrt(size);
for(let i=0;i<size;i++){
for(let j=0;j<size;j++){
let temp=document.getElementById(`${i}${j}`);
temp.parentNode.removeChild(temp);
}
}
//size=prompt("Enter Grid Size");
size=document.querySelector("#sizeDefine").value;
createGrid(+size);
}
let teableRow=createGrid(10);
const resetBtn=document.querySelector("#reset");
resetBtn.addEventListener("click", resetGridSameSize);
const sizeBtn=document.querySelector("#sizeDefineBtn");
sizeBtn.addEventListener("click", resetGrid);