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

147
Visualizações
Button in JavaScript is not working as expected

An error message appeared after adding my button which calls a function in JavaScript. I cannot find the problem.

Sorry for the faults. I am French and I'm using Google Translate.

Here is the part of the code in question:

document.getElementById("jouer").onclick = start();
document.getElementById("jouer").onclick = function () { alert('defis[0]'); };

<input id= "jouer " type="button" value="jouer" click="start()"/>;

And the error message is:

Uncaught SyntaxError: Unexpected token '<'

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It seems like you are trying to run both JS and HTML in one file.

To get it to work, you need to create a separate file with HTML stuff and use <script src="your-js-file.js"></script> in the HTML file for the browser to be able to run JS.

about 4 years ago · Juan Pablo Isaza Relatório

0

function start() {
  alert('i am working')
};

document.getElementById('jouer2').onclick = start
<input id= "jouer " type="button" value="jouer" onclick="start()"/>;
<input id= "jouer2" type="button" value="jouer2"/>;

target.onclick = functionRef;

functionRef is a function name or a function expression. so you can try this document.getElementById("jouer").onclick = start; also you don't need the click attribute in the element as you are targeting the DOM element above. if you want to trigger a function directly from some element then pass onclick attribute with the function call and you won't need to target like this document.getElementById("jouer").onclick reference: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick

about 4 years ago · Juan Pablo Isaza Relatório

0

The only error i see is that you have a whitspace in the id selector in the input "jouer". this causes the script to not be able to grab the element. Otherwise, the error Uncaught SyntaxError: Unexpected token '<' means that you are trying to include a file (script) that it can't find.

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