Script executes before DOM elements appear

I am trying to execute a really basic script which clicks a log in button in a specific page.

document.getElementsByClassName(“rc-button rc-button–secondary external-login keyclock”)[0].click();

The thing is the script is getting executed before the button appears and i am getting a button undefined . I know this because if i run the script while the page is opened already it works just fine.

My problem is that i don’t have access to files , i have access to a specific area that just has a box to enter Javascript code for that specific page.

I tried delaying the script for 30 seconds and i still get undefined.

Assuming you use Blaze put you code into the onRendered callback and you DOM elements will be in place: Templates | BlazeJS

1 Like

Hello,
so i need to find the name of the template the page is on and execute my script like this ?
Template.name.onRendered(function () {
document.getElementsByClassName(“rc-button rc-button–secondary external-login keyclock”)[0].click();
}
What if i cant find the template name ?