Link to home
Start Free TrialLog in
Avatar of dylanone
dylanone

asked on

Coding for Enter Key - Gecko Browsers

I'm trying to use the following function so that if a user uses an enter key in a Gecko based browser (FireFox, Netscape, etc) the form is submitted - right now the following doesn't work and I don't see why - I followed several examples I found already:

BTW - I'm calling the function in my form like this:
onKeyDown="EnterKeyDownHandler(event)" thus passing the event object.

function EnterKeyDownHandler(e)
{
      // process only the Enter key
      if ( (event.keyCode == 13) || (e.which.charCode == 13))
      {
      // cancel the default submit
      event.returnValue=false;
      event.cancel = true;
      document.location.href = '/results.aspx;
      return false;
      }
}
Avatar of devic
devic
Flag of Germany image

for a form better use this:
=======================
<script>
function SubmitIt()
{
     document.location.href = "http://www.sembel.net/tools/q.php?city=Thessaloniki";
     return false;
}
</script>
<form action="http://www.sembel.net/tools/q.php" onsubmit="return SubmitIt()">
      <input name=city value=Athens>
      <input type=submit>
</form>

In case you need keys manipulation then use like this:
================================
     var keyCode = window.event?window.event.keyCode:e.which;
     if(keyCode == 13)
     {
          //something
     }
Avatar of dylanone
dylanone

ASKER

Interesting item is that when the FireFox browsers has two forms on a page - it won't use the enter key - otherwise it appears all the samples I've seen thus far work. So then is their some magic to dealing with FireFox when it has two forms so that the enter key when selected works?
to be true, I don't really understand what are you talking about. It would be nice to post here what you have and what you want.
I think to explain this - the easiest way is to show the URL:

http://www.klng.com/professionals/search.aspx

If you view this in FireFox - if you type in something into the serach box in
the upper right and hit enter it doesn't run the search instead I get the
result set from the form under-neath. Now if you the serach button it works fine!

In the middle of the page - if I enter in a last name and hit the enter key
I get results back -

If I enter in a last last and pick from the drop downs even just from one of the
drop downs and hit enter - nothing happens. Nor does anything happen
if I just select one drop down and hit the enter key.  

Ideally the enter key should run the search if I hit the enter key
Also if I hit the enter key in the serach for a professional I should get results back not matter how many textboxes or drop downs I have selected

Right now this page doesn't show your code which I added in the KeyDownHandler

which on development now looks something like this:

function KeyDownHandler()
                                    
                  {
                              
                  
            var keyCode = window.event?window.event.keyCode:e.which;
                  if(keyCode == 13)
                        
                        {
                        // cancel the default submit
                        event.returnValue=false;
                        event.cancel = true;
                        document.location.href = 'searchresults.asp?qu=' + document.forms[0].qu.value;';
                        return false;
                        }
                              
                  }
                  </script>
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The only item that still I can't figure out - is why in the professional search selection - if I use any of the drop downs and hit the enter key the pages action isn't fired and so the page just stays where it is - everything else now works great!  
because you are declared onkeydown only for the input box. To control all the form you need:

<form  onKeyDown="return KeyDownHandler(event)"   .......