I am new to jQuery so I might approach this the wrong way but hope someone here can help me.
I have a pretty large HTML table that is created dynamically.
To help the user I would like to bind a navigation event to the arrow keys only for this table.
In this example I would like to "jump" from on div to the next div (they also have the class "myClass"
). The table is larger than the example below but the structure always repeats so every third td has an (editable) div.
So far I couldn't get this to work but the function does catch the arrow press as I can show alerts this way.
My jQuery:
$(document).on('keydown', '#tblCalendar', function(e){
switch(e.which) {
case 37: // left
// similar action to the left
break;
case 39: // right
$(this).closest('td').next().next().next().find('div').focus();
break;
default: return; // exit handler
}
e.preventDefault(); // prevent default action
});
My HTML (simplified):
<tbody>
<tr>
<td></td>
<td></td>
<td><div class="myClass"></div></td>
<td></td>
<td></td>
<td><div class="myClass"></div></td>
// ...
</tr>
// ...
</tbody>
Many thanks in advance, Mike