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

220
Views
How to click one button, listen for that click, and if on a specific browser... add another programmatic click?

In the snippet below, if you click on Button One, the script listens for that click, and if you're on a Chrome browser, the script will programmatically click on Button Two, and then pass a message to the console. This all works.

With each click on Button One, I'm seeing each console message doubled from the previous one... It's almost as though clicking on Button One increases the number of clicks on Button Two.

This question is two fold:

Why does the first click on Button One not return any console message?

How do I click on Button One and have it return only one click on Button Two?

let chromeCheck = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

one.addEventListener('click', function() {
  if (chromeCheck) {
    (function playVideo() {
      document.getElementById('two').click();
      two.addEventListener('click', function() {
        console.log('Button two was clicked');
      })
    })();    
  } else {
    console.log('Button two was not clicked.')
  }
})
<button class="button" id="one">Button One</button>
<button class="button" id="two">Button Two</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You got your order messed up a bit. First of all, you are clicking the button programatically before you add the event listener to button two. Therefore, you will not get a console log on the very first click. Second, you are attaching an event listener to button two on every click of button one. So everytime you click button one, another console log will be attached to button two. To avoid that, just add the event listener for button two outside the click listener for button one.

let chromeCheck = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);


if (chromeCheck) {
   two.addEventListener('click', function() {
      console.log('Button two was clicked');
   })
}

one.addEventListener('click', function() {
  if (chromeCheck) {
    document.getElementById('two').click();
  } else {
    console.log('Button two was not clicked.')
  }
})
<button class="button" id="one">Button One</button>
<button class="button" id="two">Button Two</button>

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!