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);
}
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);
}
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();
onclick that, when called, will call test with no arguments, triggering the default parameter value (which is a blank string).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();
};
c.onclick = function() { test(); }.
Eventobject itself