Skip to content

Commit 6f6fa27

Browse files
committed
change background color randomly
How to change background color randomly in a javascript function
1 parent 1811caf commit 6f6fa27

1 file changed

Lines changed: 40 additions & 80 deletions

File tree

Example/Loop/Color.html

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,57 @@
44
<head>
55
<meta charset="utf-8">
66
<title>
7-
How to Change Background Color
8-
of a Div on Mouse Move Over
9-
using JavaScript ?
7+
@codeswithpankaj.com How to change background color randomly in a javascript function
108
</title>
119

1210
<style>
13-
li{
14-
list-style: none;
15-
border: solid 1px black;
16-
height: 25px;
17-
width: 25px;
18-
border-radius: 100px;
19-
text-align: center;
20-
float: left;
21-
padding: 25px;
22-
margin: 5px;
23-
background-color: aqua;
24-
}
11+
li {
12+
list-style: none;
13+
border: solid 1px black;
14+
height: 25px;
15+
width: 25px;
16+
border-radius: 100px;
17+
text-align: center;
18+
float: left;
19+
padding: 25px;
20+
margin: 5px;
21+
background-color: aqua;
22+
}
2523
</style>
2624
</head>
2725

2826
<body>
29-
30-
31-
<lable>Enter Number : </lable>
32-
<input type="text" id="box_no">
33-
<button onclick="changeColor()">Result</button>
3427

35-
<ul id="print">
36-
</ul>
28+
<label>Enter Number: </label>
29+
<input type="text" id="box_no">
30+
<button onclick="changeColor()">Result</button>
3731

38-
<script type="text/javascript">
39-
40-
41-
const hexCharacters = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
42-
43-
44-
45-
function getCharacter(index) {
46-
return hexCharacters[index]
47-
}
48-
49-
function generateNewColor() {
50-
let hexColorRep = "#"
51-
52-
for (let index = 0; index < 6; index++){
53-
const randomPosition = Math.floor ( Math.random() * hexCharacters.length )
54-
hexColorRep += getCharacter( randomPosition )
55-
}
56-
57-
return hexColorRep
58-
}
59-
60-
61-
62-
function changeColor(){
63-
64-
// let color_box = document.getElementById("box");
32+
<ul id="print">
33+
</ul>
6534

66-
// for(let i = 0 ; i < 10 ; i++){
67-
68-
// // let color = document.getElementById("box").style.backgroundColor = generateNewColor();
69-
// color_box.innerHTML += "<li>"+generateNewColor()+"</li>";
70-
71-
72-
// }
73-
list = document.getElementById("box_no").value;
74-
75-
let print_data = document.getElementById("print");
76-
77-
for(let i = 0; i <= list ; i++){
78-
79-
80-
// print_data.style.backgroundColor += generateNewColor();
81-
print_data.innerHTML += "<li>"+i+"</li>";
82-
83-
84-
85-
86-
87-
//
88-
89-
90-
91-
}
92-
}
93-
94-
</script>
35+
<script type="text/javascript">
9536

96-
</ul>
97-
37+
function generateNewColor() {
38+
const hexCharacters = '0123456789ABCDEF';
39+
let hexColorRep = "#";
40+
for (let i = 0; i < 6; i++) {
41+
hexColorRep += hexCharacters[Math.floor(Math.random() * 16)];
42+
}
43+
return hexColorRep;
44+
}
45+
46+
function changeColor() {
47+
let list = parseInt(document.getElementById("box_no").value);
48+
let print_data = document.getElementById("print");
49+
print_data.innerHTML = ''; // Clear previous content
50+
for (let i = 0; i < list; i++) {
51+
let listItem = document.createElement('li');
52+
listItem.style.backgroundColor = generateNewColor();
53+
listItem.textContent = i + 1;
54+
print_data.appendChild(listItem);
55+
}
56+
}
57+
</script>
9858

9959
</body>
10060

0 commit comments

Comments
 (0)