-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS-Lab-4.js
More file actions
64 lines (64 loc) · 2.17 KB
/
JS-Lab-4.js
File metadata and controls
64 lines (64 loc) · 2.17 KB
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
58
59
60
61
62
63
64
//color array
var RYGB=["red","yellow","blue","green"];
//created button
document.addEventListener("DOMContentLoaded" , function(){
var btn =document.createElement("button");
btn.innerText ="Add Square";
document.body.appendChild(btn);
//add event
btn.addEventListener("click" , addSquare);
//create a div container
var container = document.createElement("div");
container.className = "container";
document.body.appendChild(container);
//create function for addSquare
function addSquare(){
var existingSquares = document.getElementsByClassName('squares').length;
var squares = document.createElement("div");
squares.className = "squares";
squares.id=existingSquares;
document.body.appendChild(squares);
//create eventListeners for squares
squares.addEventListener("click" , randoColor);
squares.addEventListener("dblclick" , removeNeighbor);
squares.addEventListener("mouseenter" , showNum);
squares.addEventListener("mouseleave" , removeNum);
//put this element in your container
container.appendChild(squares);
}
// create function randoColor
function randoColor(event){
var index = Math.floor(Math.random()*RYGB.length);
var newColor = RYGB[index];
this.style.backgroundColor = newColor;
};
//create showNum function
function showNum() {
var id = this.id;
this.innerText = id;
}
//create removeNum function
function removeNum() {
this.innerText = "";
}
//removing neighbor
function removeNeighbor() {
var id = this.id;
//sets id to identify even numbers
if (id % 2 === 0) {
// if even, remove the square after
if (this.nextSibling) {
this.nextSibling.remove() ;
} else {
alert('There are no elements to the right to remove.');
}
// if odd, remove the square before
} else {
if (this.previousSibling) {
this.previousSibling.remove();
} else {
alert('There are no elements to the left to remove.');
}
}
}
})