Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  54] [ 5]  / answers: 1 / hits: 18622  / 11 Years ago, fri, august 2, 2013, 12:00:00

In this demo, if you place your cursor in the first field and then tab out (without making any changes), the keyup event is fired on the second field. i.e., you are tabbing out of first field and into second field. Is this behavior correct? How can I prevent this from happening? Same applies to shift + tab.


Note:


a) I believe all other keys, printable and non-printable, trigger the keyup event on the first field.


b) The event isn't triggered at all if you keep the tab pressed until it moves out of both fields.


HTML:


<form id="myform">
<input id="firstfield" name="firstfield" value="100" type="text" />
<input id="secondfield" name="secondfield" value="200" type="text" />
</form>

jQuery:


jQuery(document).ready(function() {
$('#firstfield').keyup(function() {
alert('Handler for firstfield .keyup() called.');
});
$('#secondfield').keyup(function() {
alert('Handler for secondfield .keyup() called.');
});
});

More From » jquery

 Answers
6

I ended up using this solution:



HTML:



<form id=myform>
<input id=firstfield name=firstfield value=100 type=text />
<input id=secondfield name=secondfield value=200 type=text />
</form>


jQuery:



jQuery(document).ready(function () {
$('#firstfield').keyup(function (e) {
var charCode = e.which || e.keyCode; // for cross-browser compatibility
if (!((charCode === 9) || (charCode === 16)))
alert('Handler for firstfield .keyup() called.');
});
$('#secondfield').keyup(function (e) {
var charCode = e.which || e.keyCode; // for cross-browser compatibility
if (!((charCode === 9) || (charCode === 16)))
alert('Handler for secondfield .keyup() called.');
});
});


This solution doesn't run the alert if the key is tab, shift or both.



Solution: http://jsfiddle.net/KtSja/13/


[#76563] Thursday, August 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
markusdamienn

Total Points: 167
Total Questions: 119
Total Answers: 93

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;