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

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

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 Report

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 Report

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