Matej Posted August 19, 2014 Share Posted August 19, 2014 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 More sharing options...
JamesB Posted August 19, 2014 Share Posted August 19, 2014 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))'> 1 Link to comment Share on other sites More sharing options...
niche Posted August 19, 2014 Share Posted August 19, 2014 FYI, not sure this was intentional, but event != evt. Link to comment Share on other sites More sharing options...
JamesB Posted August 19, 2014 Share Posted August 19, 2014 But he's sending it as a parameter Link to comment Share on other sites More sharing options...
Matej Posted August 19, 2014 Author Share Posted August 19, 2014 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 More sharing options...
JamesB Posted August 19, 2014 Share Posted August 19, 2014 Use onkeyup instead. The element's value hasn't been updated until both functions have finished executing. 1 Link to comment Share on other sites More sharing options...
Matej Posted August 19, 2014 Author Share Posted August 19, 2014 thanks Link to comment Share on other sites More sharing options...
thescientist Posted August 20, 2014 Share Posted August 20, 2014 FYI, not sure this was intentional, but event != evt. But he's sending it as a parameter yup. he could call it foo if he wanted to. Link to comment Share on other sites More sharing options...
davej Posted August 20, 2014 Share Posted August 20, 2014 Would there be any reason to add... evt.preventDefault(); return false;} Link to comment Share on other sites More sharing options...
dsonesuk Posted August 20, 2014 Share Posted August 20, 2014 Maybe to prevent Enter key on numpad causing form submission, IF available to do so. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now