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

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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