wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Submitting a form on pressing Enter key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

Submitting a form on pressing Enter key

payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
hi all,
i have a text field called startdate.
<input type="text" name ="startDt"/>
i need to submit a form when user press enter Key.
how can this be done in jsp.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The default behaviour is that the enter key will invoke the firstnext submit button of the form in perspective of the input element. If it isn't there, or if it is the 'wrong' button in the order, then you need to add the submit button or rearrange the ordering of the submit buttons. If that isn't an option somehow, then you'll have to write some Javascript which does a form.submit() or invokes the desired submit button and attach it to the onkeypress event. The event.keyCode of the enter key is 13.
[ August 25, 2008: Message edited by: Bauke Scholtz ]
payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
i dont have submit button on the page..
Its just onEnter on the date I have to perform some search.
now how can i do that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Easiest solution: add a submit button. Make it invisible, if you want.
[ August 25, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by payal Jain:
i dont have submit button on the page..
Its just onEnter on the date I have to perform some search.
now how can i do that.

Reread the last two sentences of my answer.
Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Hi Payal,

here is total answer for your question with code snippet
just add for text field

onKeyUp event submitenterfun(this,event)


function submitenterfun(myfield,e){

var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
else
return true;

if (keycode == 13){

document.forms.loginForm.submit();
return false;
}
else
return true;
}


Thank You.
Have a Nice Time.

-Santosh
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Cleaner example:




[ August 26, 2008: Message edited by: Bauke Scholtz ]
payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
thanks for y'r replies..
It works but only when Submit button is present.
If i remove the Submit button the script dosent work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

As you have started a new topic on this matter, I am closing this discussion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submitting a form on pressing Enter key
 
Similar Threads
basic doubt
Stop Textfield from Going Blank
How to submit a form from a como box - with enter key
onkeyup function
How to handle ENTER key event in JTextArea