Can someone explain me why the submit event is never fired when pressing enter key in the input?
<form>
<input type="text" placeholder="press enter here"/>
<button disabled>disabled</button>
<button type="submit">submit</button>
</form>
Clicking on the submit button works well.
If I remove the disabled button it works well.
Tested under:
- Chrome Version 66.0.3359.181 (Build officiel) (64 bits)
- Chrome Version 68.0.3439.0 (Build officiel) canary (64 bits)
The first button is disabled but not the second which have type="submit".
Is that a known issue? Thanks,
This behavior is by design. The relevant part of the HTML5 standard is ยง4.10.21.2 "Implicit submission":
A form element's default button is the first submit button in tree order whose form owner is that form element.
If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text control is focused implicitly submits the form), then doing so for a form, whose default button has activation behavior and is not disabled, must cause the user agent to fire a click event at that default button.
The first submit button in the form is always treated as the default button, even if it is disabled. Disabling it prevents it from being used to submit the form.
Try setting the first button to type="button"
<form>
<input type="text" placeholder="press enter here" />
<button type="button" disabled>disabled</button>
<button type="submit">submit</button>
</form>
If you're not going to use the first button to submit the form, then it doesn't need to be declared as a submit button. If you don't declare it, the form likely thinks it should be the button that submits the form. (Because it's the first button the browser sees when the page form is parsed)
Related
I just spent ages trying to get Blazor not to submit and reload the page but fire off my methods instead on enter key. With
<form>
<input type="text" class="form-text" #bind-value:event="oninput" #onkeydown="Enter" #bind-value="#searchString" />
<button type="button" class="btn btn-primary" #onclick="Search">Search</button>
</form>
Whenever I hit enter the page would get reloaded if I used the enter key instead of click, preventing the results from showing. So I added
<button type="submit" disabled hidden></button>
Which fixed it.
Now this to me looks like a workaround. Is there a more elegant way to do this? If I had the original button as submit it wouldnt work either. I think this works because there is a submit button for the enter key to hit, but being disabled it cant do anything.
Using the form tag is wrong in most cases under Blazor, anyway you can find a good example of using conditionally keydown here:
https://www.syncfusion.com/faq/blazor/forms-and-validation/how-do-i-conditionally-preventdefault-inside-the-input-component-in-blazor
I have a single page React app with a simple login form with two buttons LOGIN and BACK. If I click BACK LastPass still offers to save the entered username/password, even though I didn't login.
Is there any way to tell LastPass that the back button is a cancel button for the login form and that it shouldn't try to save the username/password in that case?
HTML looks something like this:
<input name="username" type="text" />
<button type="submit">LOGIN</button>
<button>BACK</button>
You can use <input type="reset" /> or <button type="reset">.
As its name says, a reset button is ment to cancel a form. When it is activated, all user inputs are cancelled and the fields are reset back to their default values, i.e. the ones that were specified in the HTML code.
In JavaScript, You may intercept an activation of the reset button by using the reset event on the parent form, i.e. form.onreset=..., form.addEvementListener('reset', ...) or <form onreset="...">.
Note that, as for submit buttons, it's a bad practice to intercept the click event directly on the button by using onclick: although there is no universal standard way to cancel the form as there is with the enter key to submit it (escape key don't cancel the form by default), you can't be sure that there is no other way to cancel the form than click on the reset button.
I have a login page in classic asp which contains two buttons login and signup . Also i have a submit button which is hidden .On form submit i am checking the
if request.form(submit) = login
then login steps
else request.form(signup) = signup
it works fine in all browsers but in IE when i hit the enter key it esacpe the request.form(submit) = login and request.form(signup) = signup but when i click on enter button then it works fine . Any idea why i am getting nothing on the request.form(submit) = login .
The problem is pretty basic and fundamental: Internet Explorer does not have "default" submit button. Every browser is free to choose how to behave in case of submitting a form via ENTER key, modern browsers choose the logical way of sending the value of the first submit button (as if it was clicked) while IE, being IE, choose the unfriendly way. No surprise and no way to fix the root of this "problem" unless you happen to write the code for IE.
The only way around this is using JavaScript and forcing the browser to choose specific submit button upon pressing ENTER. For this, first add unique ID to the login button:
<input type="submit" name="submit" value="login" id="btnLogin" />
Now add onkeypress handler to your form tag:
<form action="YourPage.asp" method="POST" onkeypress="if (event.keyCode === 13) { document.getElementById('btnLogin').click(); return false; }">
This will emulate clicking the login button whenever ENTER is pressed inside the form.
Only downside of this approach is that if your form contains <textarea>, pressing ENTER in them will also submit the form which is not a good thing. To have them behave as they should behave add this to each of them:
<textarea onkeypress="event.cancelBubble = true;">
The above will cancel the event from bubbling upwards to the <form> keypress event.
When I have a submit button, it sends the form when I press enter.
Is the submit button necessary?
I am thinking of removing the button if it is unnecessary.
You need the submit button, otherwise the input is just an input.
Of course, you could use some javascript to force the submission, either onblur (when the input loses focus) or when the enter key is pressed.
However, I think this is a very bad idea from a user experience point of view. People expect a submit button.
Here is another SO answer with some suggestions:
Submitting a form by pressing enter without a submit button
In a <form>, one can submit by pressing the Enter key when you have a text input but hide your submit button.
<form action="wherever">
<input type="text" name="input" />
<input type="submit" value="Submit" name="submit" style="display: none;" />
</form>
This type of buttonless interface may be useful in interfaces like a command prompt, but generally, users often recognize a form with a submit button.
This bug/feature cropped up in one of my pages when viewed in google chrome so i wrote a test page which looks like this
<body>
<form action="loginhandler.php">
<input type="text" id="name">
<input type="text" id="lastname">
<input type="button" value="Login">
</form>
</body>
Here you can see, the input type is NOT of type submit. So if you press ENTER KEY on IE,Firefox,Opera, nothing happens and that is the expected behavior.
But if you press enter on chrome, it SUBMITS regardless of whether the input type is submit or not.
My question , is this a default feature/bug of chrome or am i doing something wrong here. ?
To cite section 4.10.21.2 of the HTML5 specification:
"If the form has no submit button,
then the implicit submission mechanism
must just submit the form element from
the form element itself."the form element itself."
Therefore I believe Chrome's behaviour to be correct, although I think other browsers do this as well. You can catch/block/process form submission by listening to the "submit" (e.g. to block it) event.BlockquoteBlockquotethe form element itself."
Not even Chrome, most of browsers submit once you press enter (even there is not submit button) when cursor in input.
I have the opposite problem. I use custom js-element for my form and when i use style='dispay:none;' for the submit button, chrome does not submit form on enter, although, firefox does :(