Skip to content Skip to sidebar Skip to footer

Selecting An Element By The Class Name After Insertadjacenthtml

So when I click my start button, this function is working and I can see it const displayFirstQuestion = () => { main.insertAdjacentHTML('afterbegin', `
  • Wrap the loop for registering .choice event listeners in a function and call it right after call displayFirstQuestion;
  • Query .correct and .nextButton inside answering function.

Something like this:

"use strict";
const main = document.querySelector(".main");
const container = document.querySelector(".container");
const startButton = document.querySelector(".start-button");
const questionContainer = document.querySelector(".question-container");
const choice = [...document.querySelectorAll(".choice")];
const correct = document.querySelector(".correct");
const nextButton = document.querySelector(".next-button");
const endingContanier = document.querySelector(".ending");

////////////////----------------------//////////////---------SELECTING QUESTIONS ---------//////constdisplayFirstQuestion = () => {
  main.insertAdjacentHTML(
    "afterbegin",
    `<div class="question-container">
  <h2 class="question-tag">QUESTION 1</h2>
  <h3 class="question">
    What is the keyword to define a variable that can be modified?
  </h3>
  <div class="choices">
    <p class="choice">A. Function</p>
    <p class="choice">B. Const</p>
    <p class="choice correct">C. Let</p>
    <p class="choice">D. Var</p>
  </div>
  <a href="question2.html" class="next-button">Next Question >></a>
</div>`
  );
};

startButton.addEventListener("click", () => {
  container.style.animation = "hidden 1s forwards";
  setTimeout(() => {
    container.remove();
  }, 500);
  setTimeout(displayFirstQuestion(), 501);
  setTimeout(registerEventListeners(), 600);
});

//////////////////////////////////const answering = function (element) {
  const correct = document.querySelector(".correct");
  if (!element.target.classList.contains("correct")) {
    element.target.style.background = "red";
    correct.style.background = "green";
  } else {
    correct.style.background = "green";
  }
  setTimeout(() => {
    const nextButton = document.querySelector(".next-button");
    nextButton.style.animation = "come-out 0.5s ease";
    nextButton.style.display = "inline";
  }, 300);
  choice.forEach((e) => e.removeEventListener("click", answering));
};

const registerEventListeners = function() {
  const choice = document.querySelectorAll(".choice");
  
  choice.forEach(function (e) {
    e.addEventListener("click", answering);
  });
  console.log(correct);
}
* {
  margin: 0;
  box-sizing: border-box;
}
body {
  font-family: "Source Code Pro", monospace;
  background: rgba(0, 0, 0, 0.685);
}
a {
  text-decoration: none;
  color: white;
}
h2 {
  background: #323330;
  padding: 0.5rem;
  border: 1px solid #323330;
  border-radius: 10px;
  margin-bottom: 30px;
}
.container {
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 15rem000;
  animation: come-out 1s forwards;
}
.start-button {
  font-family: "Source Code Pro", monospace;
  font-weight: 700;
  width: 250px;
  height: 55px;
  font-size: 1.6rem;
  color: #323330;
  background: rgb(240, 219, 79);
  margin: 2rem;
  cursor: pointer;
  border-bottom: 5px solid rgb(211, 191, 59);
  border-left: 5px solid rgb(211, 191, 59);
  border-right: 5px solid rgb(211, 191, 59);
  border-top: 1px solid rgb(211, 191, 59);
  transition: all 0.3s ease;
  border-radius: 10px;
}
.start-button:hover {
  color: rgb(240, 219, 79);
  background: #323330;
  border: 5px solid #323330;
}
.start-button:active {
  transform: translate(5%);
}
.question-container {
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 5rem000;
  line-height: 3rem;
  font-size: 1.1rem;
  animation: come-out 1s forwards;
}
.question {
  margin: 003%0;
  text-align: center;
}
.choices {
  flex-direction: column;
}
.choice {
  cursor: pointer;
  padding: 010px010px;
  margin-bottom: 10%;
  background: rgba(0, 0, 0, 0.432);
  border: 1px solid black;
  border-radius: 12px;
  box-shadow: 1px1px6px black;
  transition: all 0.5s ease;
  text-align: center;
}
.choice:hover {
  background: rgba(0, 0, 0, 0.232);
}
.choice:active {
  background: rgba(240, 218, 79, 0.229);
  transform: translate(5%);
}
.choice-align-left {
  text-align: left;
}

.next-button {
  position: absolute;
  top: 65%;
  font-size: 20px;
  height: 8vh;
  font-family: "Source Code Pro", monospace;
  color: white;
  cursor: pointer;
  margin-right: 15%;
  background: rgba(0, 0, 0, 0.432);
  border: 1px solid black;
  border-radius: 5px;
  box-shadow: 1px1px6px black;
  align-self: flex-end;
  display: none;
  transition: all 0.5s ease;
}
.next-button:hover {
  background: rgba(0, 0, 0, 0.232);
}
.next-button:active {
  transform: translate(5%);
}
.back-button {
  height: 3rem;
  width: 20rem;
}
@keyframes hidden {
  0% {
    opacity: 1;
  }

  100% {
    display: none;
    opacity: 0;
  }
}
@keyframes come-out {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
.q2-green {
  color: rgba(86, 221, 86, 0.596);
}
.q2-yellow {
  color: yellow;
}
<body><mainclass="main"><divclass="container"><h1>
          WELCOME TO
          <imgsrc="logo.png"alt="js-logo"width="50px"height="50px" /> QUIZ
        </h1><buttonclass="start-button">LET'S START</button></div></main><scriptsrc="script.js"></script></body>

Then you can clean up a bit some code.

Post a Comment for "Selecting An Element By The Class Name After Insertadjacenthtml"