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

597
Visualizações
JavaScript Onclick Function Fails to Execute

I have a button that triggers an onclick function:

<!DOCTYPE html>

<body>
    <h2>what can javascript do?</h2>

    <p Id="demo"></p>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
       <button type="button" onclick='document.getElementById("demo").innerHTML = "Hello Javascript!"'>Click Me!</button>
    </script>
</body>
</html>

when clicking the button, I found that the onclick does not trigger. What might be the problem with my code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • You don't need jQuery
  • Use addEventListener instead and set an ID to your button Element
  • use textContent (instead of innerHTML)
  • Add the missing <html> tags etc
  • Use type="button" on a Button Element (since by default is of type "submit")

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Demo</title>
</head>

<body>
  <h2>what can javascript do?</h2>

  <button type="button" id="demoButton">Click Me!</button>

  <p id="demo"></p>


  <script>
    const elDemo = document.querySelector("#demo");
    const elButton = document.querySelector("#demoButton");

    elButton.addEventListener("click", () => {
      elDemo.textContent = "Hello Javascript!"
    });
  </script>
</body>

</html>

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