Jump to content

2x onkeydown/onkeypress


Matej

Recommended Posts

Hi , i have input

 

 

<input id="lol" type="text" name="form_number" size="20" onkeypress='return isNumberKey(event)'>

and you can only write there numbers from numpad keyboard - i have it with this script

 

function isNumberKey(evt) {    var charCode = (evt.which) ? evt.which : evt.keyCode;    if ((charCode < 58 && charCode > 47)) {        return true;    } else {        return false;    }}

 

But what if i want it to do another function on kezdown/keypress? is it possible? for example another function , can i set keydown and at the same time keypress in the same input?

Link to comment
Share on other sites

yeh

 

I'm not sure which event will take priority for disabling the input on this one: but both functions should be called.

 

<input id="lol" type="text" name="form_number" size="20" onkeypress='return isNumberKey(event)' onkeydown='return isNumberKey(event)'>

 

 

<input id="lol" type="text" name="form_number" size="20" onkeypress='return (isNumberKey(event) && function2(event))'>
  • Like 1
Link to comment
Share on other sites

Thanks :) it helped me althought i have another "problem" .

Here http://jsfiddle.net/96eLrn6e/ it seems like the function is omitting the last number in the first input , for example

if 1st input value = 2 , the 2nd input value should = 3 , but in this case it = NaN;

but when 1st input value=22, the 2nd input value = 3 so basicly its like omitting the last number .

Link to comment
Share on other sites

FYI, not sure this was intentional, but event != evt.

 

But he's sending it as a parameter :D

 

 

yup. he could call it foo if he wanted to.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...