Search code examples
javascriptjqueryeventsonkeyup

Not getting keycode in keyup function


I'm trying to add a keyup function to an input field which is added to DOM using jquery.

$("#testDiv").html("<input id='testTextbox' name='testTextbox' type='text' onkeyup='testKeyUp(this)'>");

I want to do some operations once user enters value in testTextbox and enter key is pressed.

The keyup function is like:

function testKeyUp(e) {
alert(e.keyCode+" : "+e.which);
}

The e.keyCode and e.which is returning undefined.

How can I get the keycode in keyup function? Can anyone help me to fix this? Thanks in advance.


Solution

  • Here is What You need.

    You named the function as testKeyUp() and the real name of the function is shippingPackageKeyValueKeyUp(e). Also you wont get this but event

    JS

    $("#testDiv").html("<input id='testTextbox' name='testTextbox' type='text' onkeyup='testKeyUp(event)'>");
    
    function testKeyUp(e) {
    alert(e.keyCode+" : "+e.which);
    }