Sumbit form with Enter key and cursor search
I have a textbox on my page that the user will enter to submit. However, I don't want to have a submit button, but instead want to listen for the Enter key to submit.
The point is that this page will have multiple text boxes and I only want to send the text box that was just entered.
I know how to find out which key was pressed using jquery, but I think in my scenario I need to see if the cursor is inside a specific textbox. it is right? if so how is this achieved with jquery? In general, is my approach ok? or do I have to have a submit button?
Kem ...
a source to share
Remember that this is considered bad practice; Not only are you restricting submissions to only those with JS enabled, people expect their forms to have a submit button. This is how the Internet works. The correct way to do this is to have a shape for each of your text fields.
If you have a bunch of text boxes:
<input type='text' name='x1' class='onenter'>
<input type='text' name='x2' class='onenter'>
<input type='text' name='x3' class='onenter'>
You can do it with jQuery:
$(function() {
$('input.onenter').keyup(function(e) {
if(e.keyCode == 13) {
// enter was pressed, do whatever
// $(this) is the current textbox
}
});
});
"any" can be an asynchronous request or an actual form submission depending on your needs.
a source to share