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

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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