Modify your code to look like this:
<script type="text/javascript">
function tabE(obj, e) {
var e = (typeof event != 'undefined') ? window.event : e;
if (e.keyCode == 13) {
var ele = document.forms[0].elements;
for (var i = 0; i < ele.length; i++) {
var q = (i == ele.length - 1) ? 0 : i + 1;
if (obj == ele[i] && $(ele[q]).is(":visible")) {
ele[q].focus();
break
}
}
return false;
}
}
</script>
I just added a check to make sure that the element you are going to focus on is visible (not hidden).
EDIT: if you want to completely skip hidden fields, use the code below.
function tabE(obj, e) {
var e = (typeof event != 'undefined') ? window.event : e;
var self = $(obj),
form = self.parents('form:eq(0)'),
focusable, next;
if (e.keyCode == 13) {
focusable = form.find('input,a,select,button,textarea').filter(':visible');
next = focusable.eq(focusable.index(obj) + 1);
if (!next.length) {
next = focusable.first();
}
next.focus();
return false;
}
}
Violin:
https://jsfiddle.net/mwatz122/0zqzzmc1/
source
share