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

190
Visualizações
Call a javascript function from HTML element

I have a button on a webpage which loads after a given amount of time, works well but my client would like an easy way to add a button that calls this function so he can add dynamic values to it.

In this instance the button should remove a class after 5 seconds of the page/element loading:

Problem, the function is not being called, I have checked the reference to the javascript file is correct, I suspect the issue is the way I am calling the function, here is my code:

<a href="somewhere.com" id="myButton" window.onload='showButton("myButton", "5000")'; 
class="invisible">Now you can see me!</a>

function showButton(id, time) {
    setTimeout(function() { showButtonPrep(id, time); }, time);
    console.log("I have fired!");
}
function showButtonPrep(id, time) {
    var button = document.getElementById(id);
    button.classList.remove('invisible');
}

The console does not log the string so I can see it is not being loaded I have tried variations of the load, i.e

window.load
this.load

The javascript file containing the function is in the footer, if I put an alert in top of the javascript file that does fire.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

window.onload is not a valid HTML attribute for <a>. This seems a mixup between HTML and JavaScript.

You should put that inside a script:

window.onload = () => showButton("myButton", 5000);

function showButton(id, time) {
    setTimeout(function() {
        showButtonPrep(id, time);
    }, time);
    console.log("I have fired! Now wait 5 seconds...");
}

function showButtonPrep(id, time) {
    var button = document.getElementById(id);
    button.classList.remove('invisible');
}
.invisible { display: none }
<a href="somewhere.com" id="myButton" class="invisible">Now you can see me!</a>

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