I want to shift navigation from tab key to Enter key. For that I hooked keypress event of the form like
$("form").live("keypress", function (e) {
if (e.keyCode == 13) {
//here should come the code for finding next element and focusing it
return false; // this line prevents submission of form on enter key
}
In this function I just have to find next form element and shift focus on that one. To get next form element I used selector from this answer. But there are couple of problems with it:
- it does not cater for select lists and any other elements than input.
- it does not cater for hidden, disabled and readonly inputs
Besides this it uses index property of inputs to find next elements but the problem is when I alert all form elements with their indexes. they are not unique.
$('form input,form select').each(function (ind, elem) {
alert($(elem).attr("name"));
alert($(elem).index());
});
There are many form elements with 0 index. It is notable that some form elements are inserted in the DOM using JavaScript i.e after page load. How can I solve this problem?