I had the same requirement in my development, so I researched this. I have read many articles and tried many solutions over the past two days, such as the jQuery.tabNext () plugin.
I had some problems with IE11 (all this version of IE has this error). When the input text was accompanied by non-text input, the selection was not cleared. So I created my own tabNext () method based on the @Sarfraz solution suggestion. I also thought about how he should behave (only a circle in the current form or, perhaps, through the full document). I still did not care about the properties of tabindex mainly because I use it sometimes. But I will not forget that.
So that my contribution could be useful for everyone, I created a jsfiddle example here https://jsfiddle.net/mkrivan/hohx4nes/
I also include the JavaScript part in the example here:
function clearSelection() { if (document.getSelection) { // for all new browsers (IE9+, Chrome, Firefox) document.getSelection().removeAllRanges(); document.getSelection().addRange(document.createRange()); console.log("document.getSelection"); } else if (window.getSelection) { // equals with the document.getSelection (MSDN info) if (window.getSelection().removeAllRanges) { // for all new browsers (IE9+, Chrome, Firefox) window.getSelection().removeAllRanges(); window.getSelection().addRange(document.createRange()); console.log("window.getSelection.removeAllRanges"); } else if (window.getSelection().empty) { // maybe for old Chrome window.getSelection().empty(); console.log("window.getSelection.empty"); } } else if (document.selection) { // IE8- deprecated document.selection.empty(); console.log("document.selection.empty"); } } function focusNextInputElement(node) { // instead of jQuery.tabNext(); // TODO: take the tabindex into account if defined if (node !== null) { // stay in the current form var inputs = $(node).parents("form").eq(0).find(":input:visible:not([disabled]):not([readonly])"); // if you want through the full document (as TAB key is working) // var inputs = $(document).find(":input:visible:not([disabled]):not([readonly])"); var idx = inputs.index(node) + 1; // next input element index if (idx === inputs.length) { // at the end start with the first one idx = 0; } var nextInputElement = inputs[idx]; nextInputElement.focus(); // handles submit buttons try { // if next input element does not support select() nextInputElement.select(); } catch (e) { } } } function tabNext() { var currentActiveNode = document.activeElement; clearSelection(); focusNextInputElement(currentActiveNode); } function stopReturnKey(e) { var e = (e) ? e : ((event) ? event : null); if (e !== null) { var node = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null); if (node !== null) { var requiredNode = $(node).is(':input') // && !$(node).is(':input[button]') // && !$(node).is(':input[type="submit"]') && !$(node).is('textarea'); // console.log('event key code ' + e.keyCode + '; required node ' + requiredNode); if ((e.keyCode === 13) && requiredNode) { try { tabNext(); // clearSelection(); // focusNextInputElement(node); // jQuery.tabNext(); console.log("success"); } catch (e) { console.log("error"); } return false; } } } } document.onkeydown = stopReturnKey;
I also left commented lines, so my thinking can be done.
Miklos Krivan May 21 '17 at 9:09 a.m. 2017-05-21 09:09
source share