Advertisement
NoderCoder

Untitled

Apr 9th, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Calc for the resizing the grid
  3. let mainWrapperSize = 480; //in pixles
  4. let mainWrapperColumns = 16; //num
  5. let mainWrapperRows = 16; //num
  6. let individualRowSize = mainWrapperSize/mainWrapperRows;
  7. let individualColumnSize = mainWrapperSize/mainWrapperColumns;
  8.  
  9. //main body
  10. let mainbody = document.querySelector("body");
  11.  
  12.  
  13. let clearGridBtn = document.createElement("button");
  14. clearGridBtn.setAttribute("id","clearGrid");
  15. clearGridBtn.textContent = "Click me to clear the grid :)";
  16. mainbody.appendChild(clearGridBtn);
  17.  
  18.  
  19.  
  20. //Parent DIv Properties
  21. let parentDiv = document.createElement("div");
  22. parentDiv.setAttribute("id","mainWrapper");
  23. parentDiv.style.display = "grid";
  24. parentDiv.style.width = mainWrapperSize + "px";
  25. parentDiv.style.gridTemplateColumns = "repeat("+ mainWrapperColumns + "," + individualColumnSize + "px)";
  26. parentDiv.style.gridTemplateRows = "repeat("+ mainWrapperRows + "," + individualRowSize + "px)";
  27. parentDiv.style.border = "1px solid red";
  28. parentDiv.style.fontSize = "10px";
  29. parentDiv.style.backgroundColor = "lightblue";
  30. mainbody.appendChild(parentDiv);
  31.  
  32. // inner Child Divs properties
  33.  
  34. for (let y = 1; y <= 16; y++) {
  35.     for (var x = 1; x <= 16; x++) {
  36.         let childDiv = document.createElement("div");
  37.         childDiv.classList.add("old-background");
  38.         childDiv.setAttribute("id", ("R" + y + "C" + x));
  39.         childDiv.textContent = "R" + y + "C" + x;
  40.         childDiv.setAttribute("style", "height:auto;width:auto");
  41.         childDiv.style.border = "1px solid black";
  42.         parentDiv.appendChild(childDiv);
  43.     }
  44. }
  45.  
  46. // change color over hover function
  47. function changeColor1() {
  48.     console.log(this.id);
  49.     const cDivChange = document.getElementById(this.id);
  50.     cDivChange.style.backgroundColor = "pink";
  51.     // cDivChange.classList.add = "new-background";  
  52. }
  53. //Clear grid function
  54. function clearGrid(){
  55.     console.log(this.id);
  56.     let childDivGrabber = document.getElementsByClassName("old-background");
  57.     childDivGrabber.forEach(cDG => cDG.backgroundColor="white");
  58.     // console.log(childDivGrabber.id);
  59.     // childDivGrabber.style.backgroundColor = "white";
  60.     }
  61. // event listners
  62. const cDivs = document.querySelectorAll('.old-background');
  63. cDivs.forEach(cD => cD.addEventListener('mouseenter',changeColor1));
  64. clearGridBtn.addEventListener("click",clearGrid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement