I want to change my code to the form I could obtain switching off each of two other shapes each time I press one of these buttons. Now when I press on more than one button the shapes remain placed one on another and I want to cancel them so only one shape will be visible related to current button and when I press another button this shape should vanish. In other words, I want to swich on each shape without remain of another shape.
I was trying to change my code, but I don’t know how to do it. I was trying to apply setTimeout but this was not working as I wanted.
<code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>shapes</title>
<link rel="stylesheet" href="objectchange.css">
</head>
<body>
<button class="button-1"><div class="circle"></div>Circle</button>
<button class="button-1"><div class="rectangle"></div>Rectangle</button>
<button class="button-1"><div class="square"></div>Square</button>
<script src="objectchange.js"></script>
</body>
</html>
</code>
<code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>shapes</title>
<link rel="stylesheet" href="objectchange.css">
</head>
<body>
<button class="button-1"><div class="circle"></div>Circle</button>
<button class="button-1"><div class="rectangle"></div>Rectangle</button>
<button class="button-1"><div class="square"></div>Square</button>
<script src="objectchange.js"></script>
</body>
</html>
</code>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>shapes</title>
<link rel="stylesheet" href="objectchange.css">
</head>
<body>
<button class="button-1"><div class="circle"></div>Circle</button>
<button class="button-1"><div class="rectangle"></div>Rectangle</button>
<button class="button-1"><div class="square"></div>Square</button>
<script src="objectchange.js"></script>
</body>
</html>
<code>body{
background-color:aliceblue;
display:flex;
justify-content: center;
}
.button-1{
padding:10px 10px 10px 10px;
background-color:rgb(50,50,50);
color:white;
font-size:20px;
border-radius:15px;
margin:10px;
border:none;
}
.button-1:hover{
background-color:rgb(100,100,100);
}
.circle{
border-radius: 50%;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.rectangle{
border-radius:7px;
width:50px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.square{
border-radius:7px;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.big-circle{
position:absolute;
border-radius: 50%;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-rectangle{
position:absolute;
width:400px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-square{
position:absolute;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
</code>
<code>body{
background-color:aliceblue;
display:flex;
justify-content: center;
}
.button-1{
padding:10px 10px 10px 10px;
background-color:rgb(50,50,50);
color:white;
font-size:20px;
border-radius:15px;
margin:10px;
border:none;
}
.button-1:hover{
background-color:rgb(100,100,100);
}
.circle{
border-radius: 50%;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.rectangle{
border-radius:7px;
width:50px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.square{
border-radius:7px;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.big-circle{
position:absolute;
border-radius: 50%;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-rectangle{
position:absolute;
width:400px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-square{
position:absolute;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
</code>
body{
background-color:aliceblue;
display:flex;
justify-content: center;
}
.button-1{
padding:10px 10px 10px 10px;
background-color:rgb(50,50,50);
color:white;
font-size:20px;
border-radius:15px;
margin:10px;
border:none;
}
.button-1:hover{
background-color:rgb(100,100,100);
}
.circle{
border-radius: 50%;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.rectangle{
border-radius:7px;
width:50px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.square{
border-radius:7px;
width:30px; height:30px;
border:3px solid white;
display:block;
margin-left:auto; margin-right:auto;margin-bottom:18px;
}
.big-circle{
position:absolute;
border-radius: 50%;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-rectangle{
position:absolute;
width:400px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
.big-square{
position:absolute;
width:200px; height:200px;
border:6px solid black;
top:50%;left:50%;
transform:translate(-50%,-50%);
}
<code>const button = document.getElementsByClassName('button-1');
button[0].addEventListener('click', function(){
const circle = document.createElement('div');
document.body.appendChild(circle).classList.add('big-circle');
});
button[1].addEventListener('click', function(){
const rectangle = document.createElement('div');
document.body.appendChild(rectangle).classList.add('big-rectangle');
});
button[2].addEventListener('click', function(){
const square = document.createElement('div');
document.body.appendChild(square).classList.add('big-square');
});
</code>
<code>const button = document.getElementsByClassName('button-1');
button[0].addEventListener('click', function(){
const circle = document.createElement('div');
document.body.appendChild(circle).classList.add('big-circle');
});
button[1].addEventListener('click', function(){
const rectangle = document.createElement('div');
document.body.appendChild(rectangle).classList.add('big-rectangle');
});
button[2].addEventListener('click', function(){
const square = document.createElement('div');
document.body.appendChild(square).classList.add('big-square');
});
</code>
const button = document.getElementsByClassName('button-1');
button[0].addEventListener('click', function(){
const circle = document.createElement('div');
document.body.appendChild(circle).classList.add('big-circle');
});
button[1].addEventListener('click', function(){
const rectangle = document.createElement('div');
document.body.appendChild(rectangle).classList.add('big-rectangle');
});
button[2].addEventListener('click', function(){
const square = document.createElement('div');
document.body.appendChild(square).classList.add('big-square');
});