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

156
Views
Javascript function default parameter incorrect

Why does the code at this JS fiddle not work? I want it to log a blank string, but it's logging a Pointer Event. https://jsfiddle.net/wvom7k2f/

let c = document.getElementById('c');
c.onclick = test;

function test(a='', b=''){
console.log(a);
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

An onclick event handler will put the event as the first parameter. If you want to call test with no parameters you should change c.onclick = test; to the following:

c.onclick = () => test();
about 4 years ago · Juan Pablo Isaza Report

0

You're doing c.onclick = test. The onclick event calls its function with the event as the 1st parameter.

Try to do:

function test(e, a='', b='') {
    console.log(a);
}

c.onclick = test;

Or, if you don't want to change test(), then you can do:

function test(a='', b='') {
    console.log(a);
}

c.onclick = function(e) {
    test();
};
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!