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

227
Visualizações
How can I make 2 functions onclick work in JS?

I try to create a simple traffic light system for a project however once i use the onclick="maakGroen();maakRood();"> the 2nd function does not work....

This is my code

<input type="button" name="Licht" value="Licht" onclick="maakGroen();maakRood();">
<script>
  var Licht = document.getElementById('Licht');
  function maakRood() {
    Licht.src = "stop1.png";
  }
  function maakGroen() {
    Licht.src = "stop2.png";
  } 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use setTimeout() to delay the second function so you can see the first function's change.

<input type="button" name="Licht" value="Licht" onclick="maakGroen();setTimeout(maakRood, 1000);">
about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a third function with both functions in it and use a timeout just like @Barmar said.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try using an event listeners, in the example below, you can replace querySelector with getElementByID; more info on the developer site or you can find tutorials on w3schools https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener.

document.querySelector(".elementname").addEventListener("click", doStuff)

function doStuff() {
    maakGroen();
    maakRood(); 
}

If you don't like this, then you can simply make a new function with all the code from maakGroen();maakRood(); pasted into it.

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