How to know when an enter key press is used to select an item in thebrowser's context dropdown and not to submit a form?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Smedegaard Buus

    How to know when an enter key press is used to select an item in thebrowser's context dropdown and not to submit a form?

    Hey :)

    I'm trying to figure this one out, and it's a bit tricky.

    I have a "tip a friend" form that's submitted via AJAX. When the user
    presses enter inside one of the form's input fields, the form should
    submit. This works out-of-the-box in FF and Safari, probably others
    too, but not in IE6.

    This is why I've used the "if(event.keyCo de == 13) { try
    { event.cancelBub ble = true; event.returnVal ue = false; } catch(e)
    {}; }" trick to manually submit the form.

    However, this introduces another problem: In FF, when I write in an
    input field, I get a drop-down underneath with a list of previous
    things I entered in similar input fields (I'm guessing input fields
    with the same name tag). Now, if I pick one of these with my arrow
    keys and then press enter to select it, not only is it selected, but
    the form is also submitted, which is not the expected behavior :)

    I'm wondering if there's any way to know if such a dropdown is open
    and has focus so that I can prevent submitting the form when this is
    the case? If any of you know, please tell me :)

    Thanks in advance,
    Daniel :)
  • Daniel Smedegaard Buus

    #2
    Re: How to know when an enter key press is used to select an item inthe browser's context dropdown and not to submit a form?

    Okay guys, problem solved. This is tricky business indeed, but I ended
    up with not using the event.keyCode hack anyway.

    I've added
    form.submit(fun ction() { return false; })
    to my form ajaxifying code, which prevents IE6 from submitting the
    form on enter key presses, while real browsers work fine and submit
    via ajax on enter key, and the drop downs are indeed supported once
    more :) As I see it, if you're still using IE6, not being able to
    submit a form by hitting enter is the least of your problems ;)

    Btw, the code above is the equivalent of adding onsubmit="retur n
    false;" to the form's tag.

    Cheers,
    Daniel

    Comment

    • David Mark

      #3
      Re: How to know when an enter key press is used to select an item inthe browser's context dropdown and not to submit a form?

      On Aug 7, 3:14 am, Daniel Smedegaard Buus <danielb...@gma il.com>
      wrote:
      Hey :)
      >
      I'm trying to figure this one out, and it's a bit tricky.
      >
      I have a "tip a friend" form that's submitted via AJAX. When the user
      presses enter inside one of the form's input fields, the form should
      submit. This works out-of-the-box in FF and Safari, probably others
      too, but not in IE6.
      >
      This is why I've used the "if(event.keyCo de == 13) { try
      { event.cancelBub ble = true; event.returnVal ue = false; } catch(e)
      {}; }" trick to manually submit the form.
      I'm not familiar with that "trick." It looks like nonsense to me,
      regardless of the (missing) context. It doesn't appear to submit
      anything, manually or otherwise.
      >
      However, this introduces another problem: In FF, when I write in an
      Not a shock. Browsers don't really do tricks. It is best to
      understand the code you write.

      Comment

      • David Mark

        #4
        Re: How to know when an enter key press is used to select an item inthe browser's context dropdown and not to submit a form?

        On Aug 7, 5:11 am, Daniel Smedegaard Buus <danielb...@gma il.com>
        wrote:
        Okay guys, problem solved. This is tricky business indeed, but I ended
        Only when you use "tricks."
        up with not using the event.keyCode hack anyway.
        I don't know that hack either.
        >
        I've added
            form.submit(fun ction() { return false; })
        Sheer nonsense, any way you look at it.
        to my form ajaxifying code, which prevents IE6 from submitting the
        form on enter key presses, while real browsers work fine and submit
        IE6 is not the problem. You are the problem.
        via ajax on enter key, and the drop downs are indeed supported once
        more :) As I see it, if you're still using IE6, not being able to
        submit a form by hitting enter is the least of your problems ;)
        >
        Btw, the code above is the equivalent of adding onsubmit="retur n
        false;" to the form's tag.
        Is it?!

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: How to know when an enter key press is used to select an itemin the browser's context dropdown and not to submit a form?

          Daniel Smedegaard Buus wrote:
          I'm trying to figure this one out, and it's a bit tricky.
          It is not if one knows what one does. You do not, but hopefully that is
          going to change.
          I have a "tip a friend" form that's submitted via AJAX. When the user
          presses enter inside one of the form's input fields, the form should
          submit. This works out-of-the-box in FF and Safari, probably others
          too, but not in IE6.
          This is because you are handling a key* event of the control and not the
          `submit' of the form.
          This is why I've used the "if(event.keyCo de == 13) { try
          { event.cancelBub ble = true; event.returnVal ue = false; } catch(e)
          {}; }" trick to manually submit the form.
          Utter nonsense.
          However, this introduces another problem:
          As expected.


          PointedEars
          --
          var bugRiddenCrashP ronePieceOfJunk = (
          navigator.userA gent.indexOf('M SIE 5') != -1
          && navigator.userA gent.indexOf('M ac') != -1
          ) // Plone, register_functi on.js:16

          Comment

          Working...