Reputation: 1133
I was wondering if anyone could help me create JavaScript for Tampermonkey on Chrome that will automatically click this button when the page loads.
And here is the code for the button:
<span id="container">
<input type="hidden" name="token" value="5216d2a45d4185bf3f0d156a145e5aed543717388f11a12000855403a0ced24cbe4ec2cd5327e43dd0a6ee66690de3f54c55625cddfe31131c4e0e498893bfe0">
<input type="button" class="btn-submit btn-blue large" id="button" value="Button">
</span>
Here is one of the things that I've tried:
var button = document.querySelector (
"#button"
);
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
button.dispatchEvent (clickEvent);
and another
var btnShowObserver = function() {
var button = document.getElementsByClassName('btn-submit')[0];
button.getElementsByClassName('btn_confirm')[0].click();
}
another
function myFunction() {
document.getElementById("button").click();
}
I have read things from Choosing and activating the right controls on an AJAX-driven site to Clicking a button on a page using a Greasemonkey/userscript in Chrome
And I am still struggling I think it's something to do with the format of the button (it has no div and most code I've seen is referring to that.)
Any ideas? I've tried my best but I am not overly familiar with javascript
Upvotes: 1
Views: 6684
Reputation: 1852
The code from your question,
function myFunction() {
document.getElementById("button").click();
}
will work.
I am assuming that since it is inside a function
, you forgot to call the function with myFunction()
.
Or, the easier way to do this, is to remove the function altogether, and change it to the following.
document.getElementById("button").click();
Upvotes: 2