Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Hover effect is not working after calling a function that changes that inner text of target elements

I have a problem: Hover effect is not working after calling a function that changes the inner text of target elements. Basically, after loading the page it's working, but once I click a 'start' button which calls a function to change the content of the divs, hover effect is no longer working. Here is the code:

  <div class="options">
    <div class="option" id="opt1"><div>A: one</div></div>
    <div class="option" id="opt2"><div>B: two</div></div>
    <div class="option" id="opt3"><div>C: three</div></div>
    <div class="option" id="opt4"><div>D: four</div></div>
  </div>
    <button class="button" onclick="loadQuestions()">Start</button>

  .option {
  border: 2px solid blue;
  }

 .option :hover {
  box-shadow: 0 0 0 3px burlywood;
  cursor: pointer;
  }

  const question = document.getElementById('question');
  const opt1 = document.getElementById('opt1');
  const opt2 = document.getElementById('opt2');
  const opt3 = document.getElementById('opt3');
  const opt4 = document.getElementById('opt4');

  function loadQuestions() {
 question.innerText = 'How old are you?';
 opt1.innerText = 'A: 55';
 opt2.innerText = 'B: 52';
 opt3.innerText = 'C: 82';
 opt4.innerText = 'D: 12';
 }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't have a tag with id question so its throwing an error. Checkout following 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>Document</title>
    <style>
      .option {
        border: 2px solid blue;
      }

      .option:hover {
        box-shadow: 0 0 0 3px burlywood;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div class="options">
      <span id="question" class="question"></span>
      <div class="option" id="opt1"><div>A: one</div></div>
      <div class="option" id="opt2"><div>B: two</div></div>
      <div class="option" id="opt3"><div>C: three</div></div>
      <div class="option" id="opt4"><div>D: four</div></div>
    </div>
    <button class="button" onclick="javascript:loadQuestions()">Start</button>

    <script type="text/javascript">
      const question = document.getElementById("question");
      const opt1 = document.getElementById("opt1");
      const opt2 = document.getElementById("opt2");
      const opt3 = document.getElementById("opt3");
      const opt4 = document.getElementById("opt4");

      const loadQuestions = () => {
        question.innerText = "How old are you?";
        opt1.innerText = "A: 55";
        opt2.innerText = "B: 52";
        opt3.innerText = "C: 82";
        opt4.innerText = "D: 12";
      };
    </script>
    <script src="./index.js" type="text/javascript"></script>
  </body>
</html>

You have to specify a tag with question id.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda