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

423
Views
Is there a way to simulate a website button click without taking focus?

I have the following code:

var parentAction = document.querySelector("[title='Actions for Tab']");
var refreshButton = parentAction.getElementsByClassName("slds-truncate")[0].click();

This causes a specific tab to refresh on a website. I have it set up in a function with an interval set to run every 60 seconds. This runs without any issues.

However, a lot of the time this function will run while a user is typing in an input field. When the click happens it takes focus from the user typing and causes random windows to open on the page because of the keys they press. If they're not typing at the time, it's fine. They just have to click back into the input field and continue. This does get frustrating after a short time though.

Is there a better way to simulate the click event without taking focus from the input field they're typing in?

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

0

If the tab refreshes through ajax then why not just call a method triggering the refresh action.

If you have to go that route: The following is workaround that bypasses the refresh amidst user action; it also includes a timer that kicks if user leaves during form dabblings.

var i = document.querySelector('input')
var inputFocus = false, pageTimer;
 
i.onfocus = () => {

    inputFocus = true;
    pageIdle()
}
  
i.onblur = () => { pageIdle() }


let refreshPage = () => {

   if (!inputFocus) console.log('page refresh')
}

let pageIdle = () => {
 
   console.log('user typing')
   if(pageTimer) clearTimeout(pageTimer)
   pageTimer = setTimeout(() => { inputFocus = false}, 6000)   <<  refreshes when users idles
}

setInterval(refreshPage, 2000)    <<  refreshes regularly 

document.onkeydown = () => {

   inputFocus = true 
   pageIdle()
}

https://jsfiddle.net/7co5yjnb/1/

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!