jQuery: Enable Tab + Shift-tab only in modal dialog element

Date: 2018-04-10
var dialog = $('.modal');
dialog.on('keydown', function(e) {
	var tab = 9;
	if (e.which === tab) {
		e.preventDefault();
		var inputs = $('input, a, button', dialog).filter(':visible');

		var ci = 0;
		inputs.each(function(i) {
			if ($(this)[0] == document.activeElement) {
				ci = i;
			}
		});		
		var step = 1;
		if (e.shiftKey) step = -1;
		ci += step;
		if (ci < 0) ci += inputs.length;
		inputs[ci % inputs.length].focus();
	}
});

 

10860cookie-checkjQuery: Enable Tab + Shift-tab only in modal dialog element