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

193
Visualizações
Why does the click event of JavaScript not working?

I got this link from a friend of mine for testing my reaction time. https://www.arealme.com/reaction-test/en/

This is a game where a red circle will appear and after random time it will turn green. I need to click as soon as it turns green. That is my reaction time.

With real reaction, my best score is 178, in case you don't believe I have attached ss enter image description here

I know I can edit this 178 to any number I want. But I wanted to create a script that generated the click event as soon as the green circle appears.

I have planned to call the below function with setInterval

const clickCircle = (className)=> {
    let greenCircles = document.getElementsByClassName(className);
    if(greenCircles && greenCircles.length){
        greenCircles[0].click();
    }else{
        console.log("No circles found with name ", className)
    }
}

clickCircle("tfny-circleGreen");

The name of the class containing the green circle is tfny-circleGreen. So calling clickCircle("tfny-circleGreen") should generate a click event. But it's not.

What am I missing?

Maybe they have overridden the click function. If that's the case, how do I restore it?

Please don't ask why I want this, it's just I wanted to try it this way for fun.

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

0

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

const clickCircle = (className)=> {
    let greenCircles = document.getElementsByClassName(className);
    if(greenCircles && greenCircles.length){
        triggerMouseEvent (greenCircles[0], "mouseover");
        triggerMouseEvent (greenCircles[0], "mousedown");
        triggerMouseEvent (greenCircles[0], "mouseup");
        triggerMouseEvent(greenCircles[0], "click");
    }else{
        console.log("No circles found with name ", className)
    }
}

setInterval(()=> {
    clickCircle("tfny-circleGreen");
}, 10)

This is the complete code that works :D

The click() function was not working so I had to simulate it.

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