Form submit button not having click event in browser, yet form is getting submitted?
Both on right click and left click?
And only when submit button is clicked, nowhere around the form.
Any explanation for it? Can you just submit form without Click event?
10 Replies
if the button is of type="sumbit", then yes, it will try to send the form
It is.
So? It reacts on click but it has no event listener?
Liek a native built in HTML listener?
that is native behaviour yes. it will try to do the action that is set in the form element
if you dont want that, create and event listener on formsubmit or button click, and do event.preventDefault
So I was testing automation on a random website, and put button.click() but it did not work, or I did something wrong? IS my code right?
button.click() is nothing
button.onClick(()=>{...}), or button.addEventListener("click", ()=> { ...} )
I was automating my code to click it, like you would with mouse. Is it still wrong?
Hence the button.click();
no, that won't work like that.
you'll have to simulate a mouse click with a dispatch event in order to do that
( I think, never tried)
you might try this
https://javascript.plainenglish.io/how-to-simulate-a-mouse-click-using-javascript-baf1620514fd
Medium
How to Simulate a Mouse Click Using JavaScript
We can trigger a mouse click event by using the MouseEvent constructor.
Oh okay!
Will read through it tomorrow and see how it goes!
Thanks a lot!
good luck