Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!