Onkeydown event for html form doesn't fire in IE 7 or FF 3

I am writing an asp.net application and have a form where the user has to press a key to make the text box visible so he can login. When the key is pressed, I want the label to disappear and the textbox to become visible. For some reason, the onkeydown event does not fire in FF or IE, but works fine in Chrome. The app will run on an AML terminal using the Links browser, but I can't test it on that platform right now. Here is my code:

<form id="form1" runat="server" onkeydown="CheckKey(event.keyCode)" 
enableviewstate="True" submitdisabledcontrols="False" visible="True">

<script type="text/javascript">

    function SetVisibility() {
        var txtbx = document.getElementById("txtbx_login")
        txtbx.style.display = "none";
        var form = document.getElementById("form1")
    }

    function CheckKey(keycode) {
        if (keycode == 113) {
            var txtbx = document.getElementById("txtbx_login")
            txtbx.style.display = "";
            var lbl = document.getElementById("lbl_login")
            lbl.style.display = "none";
        }
    }

</script>

      

****** other form elements ******

</form>

      

The only way to get the onkeydown event is to work if the textbox control is visible and has focus. Am I missing something? Thanks for the help!

0


a source to share


1 answer


I think the problem is that the onkeydown check is being done on the form.

Try moving it to the body element, or better, use javascript to add an event handler to the window event:



function handleKeypress(e){

       var keycode = e.keyCode || e.charCode;

       if (keycode == 113) {
            var txtbx = document.getElementById("txtbx_login")
            txtbx.style.display = "";
            var lbl = document.getElementById("lbl_login")
            lbl.style.display = "none";
        }

    }
    window.onkeypress = handleKeypress;

      

In firefox, you also need event.charCode. I haven't tested the above in all browsers, but this is a start.

+3


a source







All Articles