I want to create 31 buttons with the option to change the background color of each individual button. The problem is when I try to change the color of button 2, it changes the color of button 1.
What i am trying to do is in the picture Cross.
function myFunctionRed()
{
document.getElementById("myBtn").style.background = "green";
}
function myFunctionGreen()
{
document.getElementById("myBtn").style.background = "yellow";
}
function myFunctionBlue()
{
document.getElementById("myBtn").style.background = "red";
}
// Get the modal
var modal = document.getElementById("myModal");
var modal2 = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var btn2 = document.getElementById("myBtn2");
// Get the <span> element that closes the modal
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
{
modal.style.display = "block";
}
btn2.onclick = function()
{
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function()
{
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
{
if (event.target == modal)
{
modal.style.display = "none";
}
}
body
{
font-family: Arial, Helvetica, sans-serif;
}
.modal
{
display:none;
background:#efefef;
border:1px solid black;
width:240px; height:100px;
}
.close
{
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus
{
color: #000;
text-decoration: none;
cursor: pointer;
}
#myBtn, #myBtn2
{
background-color:gray;
border: 0.5px solid black;
color: white;
width:30px;
height:30px;
border-radius: 10%;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
#demo1
{
background-color:green;
border: none;
color: white;
width:60px;
height:60px;
border-radius: 50%;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
#demo2
{
background-color:yellow;
border: none;
color: white;
width:60px;
height:60px;
border-radius: 50%;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
#demo3
{
background-color:red;
border: none;
color: white;
width:60px;
height:60px;
border-radius: 50%;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<button id="myBtn">1</button>
<button id="myBtn2">2</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<button id="demo1" onclick="myFunctionRed()">Red</button>
<button id="demo2" onclick="myFunctionGreen()">Green</button>
<button id="demo3" onclick="myFunctionBlue()">Blue</button>
</div>
</div>