Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 4x4 divs in the body,when i click on any div, it's generating random numbers in that div 0-100 numbers, like this http://prntscr.com/k5p0hr[^]

If numbers are coinciding, i need to draw with any color that divs. For example if there are three 26 numbers in div, then that three div's background color should be changed.

What I have tried:

I have tried like this, but it's wrong.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Das 7</title>
	<style>
			.parent{
			overflow: hidden;
		}
		.d2{
			float: left;
			margin: 1px;
			outline: 1px solid red;
			width: 100px;
			height: 100px;
		}
	</style>
</head>
<body>
	<div>
		<div class="parent">
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
		</div>
		<div class="parent">
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
		</div>
		<div class="parent">
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
		</div>
		<div class="parent">
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
			<div class="d2"></div>
		</div>
	</div>
</body>
<script src="JS/script.js"></script>
</html>



var arr = document.querySelectorAll(".d2");
for(var i = 0; i < arr.length; i++){
	arr[i].addEventListener("click", F);
}

function F(){
	var r = parseInt(Math.random()*100);
	this.innerHTML = r;
	if(arr.innerHTML == r){
		arr[i].style.background = "red";
}
}
Posted
Updated 12-Jul-18 17:34pm

1 solution

Based on what posted here, more works are required. Here is an example: div bg color[^]

The arr is an array, you need to include a loop to iterate the object in it. Then you also need to add some logic to update the background color of other cells if the same number exists.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900