When I use method click()
to click the button, the submission failed.
But, when I use the mouse to click the button on that webpage, (the submit form is same) it works.
HTML file:
<button xmlns="http://www.w3.org/1999/xhtml" class="ui-button button1" type="submit" id="create">
<span>
<span>Create</span>
</span>
</button>
My userscript file:
document.getElementById("create").click();
How do I click the button using HTML DOM click()?
The click method does not always work. Try sending a click
event similarly to this answer:
var targBtn = document.querySelector ("#create");
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
targBtn.dispatchEvent (clickEvent);
But also beware that the button may not operate on clicks at all. See "Choosing and activating the right controls on an AJAX-driven site".
The button may operate on mousedown
, mouseup
, or some combination of events. Use the techniques from that question to activate the control. Link to the target page if you can't get it to work.