Search code examples
javascripteventskeydownonkeydown

Event not defined error, only in firefox


I have a code below which tells you which keys you pressed, when you type in the textarea, and it works in Google Chrome. However, in Firefox, when you pressed a key, there is a event not defined error. How would we fix this?

<textarea onkeydown = "checkKey()"></textarea>

<script>

function checkKey() {

var key = event.keyCode;
alert(key);

} //end of checkKey()

</script>

P.S: I don't want jQuery solutions. Pure JavaScript only. Unless we can't do it in pure JavaScript. Then I'll accept jQuery solutions.


Solution

  • event is defined in chrome, but not in firefox. You can do pass event as an argument.

    <textarea onkeydown = "checkKey(event)"></textarea>
    
    <script>
    
    function checkKey(event) {
    
    var key = event.keyCode;
    alert(key);
    
    } //end of checkKey()
    
    </script>