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

240
Visualizações
Uncaught ReferenceError : I am trying to use a button with OncLick method in JavaScript but keep getting error ,

I am trying to get a message when the button is clicked but I keep getting errors in my console and the button is not working. It's my first week in learning js.

The error I am getting in console is:

Uncaught ReferenceError: purchase is not defined at HTMLButtonElement.onclick (shoe.html:10:32)

let showMessage = document.getElementById("error=el");
let message = " sorry some error!"
function purchase(){
  //console.log('clicked');
  showMessage.textContent = message;
}
purchase();
<!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>
</head>
<body>
  <button onclick="purchase()">Purchase</button>  
  <p Id="error-el"></p>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are some small mistakes in the code that I fixed,

  • The id attribute should be in lower case
  • In getting element by Id function you're looking for error=el instead of error-el. The problem here was the = instead of -

let showMessage = document.getElementById("error-el");
let message = " sorry some error!"
function purchase(){
  showMessage.textContent = message;
}
<!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>
</head>
<body>
  <button onclick="purchase()">Purchase</button>  
  <p id="error-el"></p>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

To use JavaScript in your HTML, you should load the script from another file using a <src> tag or write it directly into the .html file with a <script> tag.

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