This site must do the following things:
- I type "MERAVIGLIA" and click on the first button, and the first door must open.
- I do the same things with the rest of the WORDS and all the doors must open one after another.
But it doesn’t work: the first door opens but the following ones don’t.
I don’t know how to fix it!
Suggestions?
<html>
<head>
<title>
Porta
</title>
<style>
.backDoor
{
background-color: rgb(74, 52, 25);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
margin: 0 auto;
margin-top:100px;
}
.door
{
background-color: rgb(74, 52, 25);
background-image: url(porta.png);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
margin: 0 auto;
margin-top:100px;
transform-origin: left;
/*Speed of the Door animation*/
transition: all 0.5s ease-in-out;
}
.doorOpen
{
/*prespectiv creates the door open effect*/
transform: perspective(1200px) translateZ(0px) translateX(0px) translateY(0px) rotateY(-110deg);
}
body {
background-color: black;
background-image: url(parete.jpg);
}
.door2 {
background-color: rgb(74, 52, 25);
background-image: url(porta.png);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
transform-origin: left;
margin: 0 auto;
margin-top:100px;
/*Speed of the Door animation*/
transition: all 0.5s ease-in-out;
}
.door3 {
background-color: rgb(74, 52, 25);
background-image: url(porta.png);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
transform-origin: left;
margin: 0 auto;
margin-top:100px;
/*Speed of the Door animation*/
transition: all 0.5s ease-in-out;
}
.door4 {
background-color: rgb(74, 52, 25);
background-image: url(porta.png);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
transform-origin: left;
margin: 0 auto;
margin-top:100px;
/*Speed of the Door animation*/
transition: all 0.5s ease-in-out;
}
.door5 {
background-color: rgb(74, 52, 25);
background-image: url(porta.png);
position:center;
top:0px;
left:0px;
width:250px;
height:540px;
transform-origin: left;
margin: 0 auto;
margin-top:100px;
/*Speed of the Door animation*/
transition: all 0.5s ease-in-out;
}
</style>
</head>
<body>
<div class="backDoor">
<div class="door5">
<div class="door4">
<div class="door3">
<div class="door2">
<div class="door">
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<input type="text" id="parola">
</input>
<button id="button1" onclick="toggleDoor()">
Apri prima porta
</button>
<button id="button2" onclick="toggleDoor2()">
Apri seconda porta
</button>
<button id="button3" onclick="toggleDoor3()">
Apri terza porta
</button>
<button id="button4" onclick="toggleDoor4()">
Apri quarta porta
</button>
<button id="button5" onclick="toggleDoor5()">
Apri quinta porta
</button>
</div>
<script>
var parola=document.getElementById("parola").value;
var element = document.querySelector(".door");
// element.addEventListener("click", toggleDoor)
function toggleDoor() {
if(parola=="MERAVIGLIA") {
element.classList.toggle("doorOpen");
}
}
var element2 = document.querySelector(".door2");
// element.addEventListener("click", toggleDoor)
function toggleDoor2() {
if(parola=="FIDUCIA") {
element2.classList.toggle("doorOpen");}
}
var element3 = document.querySelector(".door3");
// element.addEventListener("click", toggleDoor)
function toggleDoor3() {
if(parola=="ACCOGLIENZA") {
element3.classList.toggle("doorOpen"); }
}
var element4 = document.querySelector(".door4");
// element.addEventListener("click", toggleDoor)
function toggleDoor4() {
if(parola=="BUONENOTIZIE") {
element4.classList.toggle("doorOpen");}
}
var element5 = document.querySelector(".door5");
// element.addEventListener("click", toggleDoor)
function toggleDoor5() {
if(parola=="CAMMINAREINSIEME") {
element5.classList.toggle("doorOpen");
}}
</script>
</body>
<footer>
</footer>
</html>
2
Answers
The problem is that you are getting the value when calling getElementById
which happens when the script starts running and the value is still null (input is empty).
you need to remove the .value from there and put it inside the toggleDoor functions. such as :
your full code would be:
I simplified your code (CSS and JS) somewhat.
.door
classes had the same property values. By using[class^="door"] { ... }
(select all class attributes starting with ‘door’) you can simplify the CSS. I left thebackground-color
andbackground-image
in place, assuming you will want to change those per door.onclick
attribute, you can easily use that to construct the Javascript reference to a specific door, check the password and toggle the door.