I am trying to create a form where, if you press enter key on the serial column, the cell below bees selected. Can someone please solve the correct jquery formula for this task?
I am trying
$('.serial').keypress(function(e) {
console.log(this);
if (e.which == 13) {
$(this).nextAll('#serial').first().focus();
e.preventDefault();
}
});
/
I am trying to create a form where, if you press enter key on the serial column, the cell below bees selected. Can someone please solve the correct jquery formula for this task?
I am trying
$('.serial').keypress(function(e) {
console.log(this);
if (e.which == 13) {
$(this).nextAll('#serial').first().focus();
e.preventDefault();
}
});
http://jsfiddle/tYFcH/4/
Share Improve this question edited Jan 23, 2013 at 1:29 Jeff 12.8k5 gold badges35 silver badges61 bronze badges asked Jan 23, 2013 at 1:20 user2002232user2002232 231 silver badge5 bronze badges 1-
1
'#serial' !== '.serial'
– epascarello Commented Jan 23, 2013 at 1:23
3 Answers
Reset to default 5Assuming you want to move to the cell below when hitting enter in any input, use this:
$('table input').keypress(function(e) {
if (e.keyCode == 13) {
var $this = $(this),
index = $this.closest('td').index();
$this.closest('tr').next().find('td').eq(index).find('input').focus();
e.preventDefault();
}
});
Here's your fiddle: http://jsfiddle/tYFcH/13/
$('.serial').keypress(function(e) {
console.log(this);
if (e.which == 13) {
$(this).closest('tr').next().find('input.serial').focus();
e.preventDefault();
}
});
http://jsfiddle/tYFcH/6/
The next-in-dom plugin was written for this purpose. Disclaimer: I wrote it
http://techfoobar./jquery-next-in-dom/
$('.serial').keypress(function(e) {
if (e.which == 13) {
$(this).nextInDOM('.serial').focus();
}
});