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

266
Views
js code working in console but not in vs code

I'm a beginner, learning js. I was doing a course and the task provided was that to pop an alert when the button is clicked. Pretty Ez but everytime somehow the code just doesn't works even when I typed the code seeing the solution. Checked pretty much everything within my knowledge but couldn't figure it out. while trying the same thing in another computer, it worked without any problem :/Here's the code

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Window.alert is not native JS. It is a Web API method, meaning your browser source code implements it.

If you're running, say, a Node.js environment in VSCode's console, then it shouldn't really work. Node doesn't implement it either.

about 4 years ago · Santiago Gelvez Report

0

I have made an example here on a code pen: https://codesandbox.io/s/buttontoalert-fs1kz3

What you need to do is create an index.html that is importing the script you want to run.

<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <button>Click Me</button>

    <script src="src/index.js"></script>
  </body>
</html>

Then in your index.js file which is the script you are importing in the html file the code is:

const button = document.querySelector("button");

button.addEventListener("click", () => alert("I was clicked"));

These two file need to be in the same directory or you need to update the file path to where the js file is located relative to your html file.

The commands you are using a relative to a browser so need to be run with a browser or will not work.

about 4 years ago · Santiago Gelvez 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!