Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 1st, 2008, 06:33 PM
Member
 
Join Date: Jun 2007
Posts: 94
Default IE Enter Key not working on form

Hi All,

I have a feedback form in my project. When I click on first or any field in form and press enter in Firefox the form get submitted but in IE6 its not.

In IE When I scroll down the form and get return to first field and hit enter it gets submitted. It's a strange problem.

Please help me, why it is happening

Regards,
Reply
  #2  
Old September 2nd, 2008, 12:39 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,760
Default

Hi.

Each browser will handle the enter key differently. You can't really depend on the browsers default behavior if you want consistent cross-browser behavior.

You could have your <input> onkeypress event call a function that would check if the enter key was pressed and submit the form.

Something like:
[code=javascript]
<script type="text/javascript">
function InputOnKeyPress(event)
{
// Get the keycode
var keycode = 0;
if (event) // Standard method
keycode = event.which;
else if (window.event) // IE workaround
keycode = window.event.keyCode;
else // Something even weirder than IE
return false;

// Check if it is enter
// Keycode 13 is the enter key
if(keycode == 13) {
document.getElementById('myForm').submit();
}
}
</script>
[/code]
Reply
  #3  
Old September 2nd, 2008, 03:31 AM
Member
 
Join Date: Jun 2007
Posts: 94
Default

Thanks for the script :)
But this script is checking on every key pressed by the user on any field of form...don't you think this is not right?

And will this script handle the following browsers:
-IE
-FF
-Safari
Reply
  #4  
Old September 2nd, 2008, 03:15 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,760
Default

Personally I wouldn't use a method like this.
If people choose to use a browser that doesn't allow features like this, it's their problem. We can't really code around ever single eccentricity in every single browser (try as we might).

If your concerned about potential performance issues, don't be.
A simple check like this won't even register on any performance monitor, no matter how fast a person might type.
(From the computers perspective, a key-stroke is an extremely rare occasion.)

And if I made it unclear in my previous post, this would have to be manually added to each <input>, so you would have to choose which once you want checked.

The code will work on any standards-supporting browser (Firefox, Opera, Safari, etc.). And I also added a fix to make it work on IE. (Which I thought I made apparent in the comments.)
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles