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

478
Views
Why button.click() is not working in my script?

I have a script in JavaScript that in a part of the code create a 'virtual button' and press it automatically.

This button is not in the HTML page and is only used to store a link that at a certain point of the script must be clicked. Here's the code:

var virtualButton = document.createElement("button");
var linkText = document.createTextNode("assign to relevant user");
virtualButton.appendChild(linkText);
virtualButton.title = "assign to relevant user";
virtualButton.href = link;
document.body.appendChild(virtualButton);

virtualButton.click();

Everything seems fine to me, still the code does not actually click the button.

Am I missing something here?

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

0

<button/> elements do not have an .href attribute. You most likely want to use an anchor (<a/>) element. See below:

var virtualButton = document.createElement("a");
var linkText = document.createTextNode("assign to relevant user");
virtualButton.appendChild(linkText);
virtualButton.title = "assign to relevant user";
virtualButton.href = "https://www.google.com";
document.body.appendChild(virtualButton);

virtualButton.click();

(It won't actually navigate because it is in a sandboxed <iframe/>, but you will see it attempt to do so).

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!