-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathColor.html
More file actions
61 lines (51 loc) · 1.31 KB
/
Color.html
File metadata and controls
61 lines (51 loc) · 1.31 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
@codeswithpankaj.com How to change background color randomly in a javascript function
</title>
<style>
li {
list-style: none;
border: solid 1px black;
height: 25px;
width: 25px;
border-radius: 100px;
text-align: center;
float: left;
padding: 25px;
margin: 5px;
background-color: aqua;
}
</style>
</head>
<body>
<label>Enter Number: </label>
<input type="text" id="box_no">
<button onclick="changeColor()">Result</button>
<ul id="print">
</ul>
<script type="text/javascript">
function generateNewColor() {
const hexCharacters = '0123456789ABCDEF';
let hexColorRep = "#";
for (let i = 0; i < 6; i++) {
hexColorRep += hexCharacters[Math.floor(Math.random() * 16)];
}
return hexColorRep;
}
function changeColor() {
let list = parseInt(document.getElementById("box_no").value);
let print_data = document.getElementById("print");
print_data.innerHTML = ''; // Clear previous content
for (let i = 0; i < list; i++) {
let listItem = document.createElement('li');
listItem.style.backgroundColor = generateNewColor();
listItem.textContent = i + 1;
print_data.appendChild(listItem);
}
}
</script>
</body>
</html>