Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda