(function($){
var getTabStops = function(o, a, el) {
// Check if this element is a tab stop
if (el.tabIndex > 0) {
if (o[el.tabIndex]) {
o[el.tabIndex].push(el);
} else {
o[el.tabIndex] = [el];
}
} else if (el.tabIndex === 0) {
// Tab index "0" comes last so we accumulate it seperately
a.push(el);
}
// Check if children are tab stops
for (var i = 0, l = el.children.length; i < l; i++) {
getTabStops(o, a, el.children[i]);
}
}
focusNext = function() {
var o = [],
a = [],
stops = [],
active = document.activeElement;
getTabStops(o, a, document.body);
// Use simple loops for maximum browser support
for (var i = 0, l = o.length; i < l; i++) {
if (o[i]) {
for (var j = 0, m = o[i].length; j < m; j++) {
stops.push(o[i][j]);
}
}
}
for (var i = 0, l = a.length; i < l; i++) {
stops.push(a[i]);
}
// If no items are focusable, then blur
if (stops.length === 0) {
active.blur();
return;
}
// Shortcut if current element is not focusable
if (active.tabIndex < 0) {
stops[0].focus();
return;
}
// Attempt to find the current element in the stops
for (var i = 0, l = stops.length; i < l; i++) {
if (stops[i] === active) {
if (i + 1 === stops.length) {
active.blur();
return;
}
stops[i+1].focus();
return;
}
}
// We shouldn't make it this far
active.blur();
}
$.fn.enterAsTab = function(){
var that =this;
this.on('keypress',function(e) {
if(e.keyCode==13){
focusNext();
}
return false;
}
);
}
}(jQuery))
$('.blah').enterAsTab();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.