Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  172] [ 3]  / answers: 1 / hits: 32170  / 14 Years ago, fri, march 4, 2011, 12:00:00

I have two text fields in my web page. The user is supposed to enter two numbers seperated by a hyphen (-) character. The numbers may be between 1 and 10 digits each. I need the cursor to move to the next field when the user presses the hyphen key.



I can easily move the cursor using $('#txtField2').focus(). However, I still have the problem that the hyphen character remains in the first text field. How can I easily supress the hyphen from appearing in the first text field?


More From » jquery

 Answers
20

HTML



<form>
<input type='text' class='num' />
<input type='text' class='num' />
</form>


JavaScript



$('.num:first').keydown(function (event) {
// check for hyphen
if (event.which === 189) {
event.preventDefault();
$(this).next('.num').focus();
}
});


Live demo


[#93429] Thursday, March 3, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alfredoc

Total Points: 261
Total Questions: 128
Total Answers: 89

Location: French Polynesia
Member since Sun, Aug 2, 2020
4 Years ago
;