Gaurravs
Gaurravs

Reputation: 689

How to use $(this) on keypress

I have a method defined on keyPress event of input. Same method is called on multiple input controls.

I need to read value in the current control. I am not able to use this keyword. Can someone help, how can I use this keyword.

Script:

function isNumberKey(e,obj) {
    var keyCode = e.which ? e.which : e.keyCode
    var ret = ((keyCode >= 48 && keyCode <= 57) || keyCode == 46);
    var inp = $(this).val();
    alert(inp);
    // rest code goes here
    return ret;
}

HTML :

<input type="text" id="inpt1" onkeypress="javascript: return isNumberKey(event,this)"/>
<input type="text" id="inpt2" onkeypress="javascript: return isNumberKey(event,this)"/>

Upvotes: 0

Views: 165

Answers (1)

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

You can accsess obj as:

var inp = $(obj).val();

Upvotes: 2

Related Questions