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

192
Views
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 answers
Answer question

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