0

I have the below HTML input type Number

HTML:

<input type=“number” class=“reqField” id=“number1” placeholder=“Enter Number only/>
<input type=“number” class=“reqField” id=“number2” placeholder=“Enter Number only/>

JS:

function focusInNumber (id) {
var thisID = id;
var nextID = id + 1;
var preID = id - 1;
clearTimeout(numberReturn);
$(“#number” + thisID).prop(“disabled”, false);
placeCursor($(“#number” + thisID));
}
function focusOutNumber (id) {
var thisID = id;
var nextID = id + 1;
var preID = id - 1;
var value = $(“#number” + thisID).val();
var regex = new RegExp(/^\d*$/);
var regex1 = new RegExp(/^.*[\+\-\.].*/);
var l = $(“#number” + thisID).val().length;
if(value.match(regex3)) {
alert(“Just enter numerical digits”);
numberReturn = setTimeout(function() {
placeCursor($(“#number” + thisID));
},5000);
} else {
if (l<=0) {
alert(“This field cannot be empty”);
placeCursor($(“#number” + thisID));
},5000);
} else {
if(value.match(regex)) {
placeCursor($(“#number” + nextID));
}
}
}
function placeCursor(id) {
id.focus();
//id.val(id.val());
var tmp= id.val();
id.val(“”);
id.va(tmp);
//id.focus().val(“”).blur().focus().val(tmp);
}

$(document).ready(function(){
....
$(“#number1”).focusin(function(){
focusInNumber(1);
});
$(“#number1”).focusout(function(){
focusOutNumber(1);
});
...
});

So the problem is that every time tab is pressed, the next text box is focused but the cursor is not in it. I have to click on it to type. I can’t figure out why it’s behaving like this on chrome and IE.

As chrome selection is only permitted with type text/search, url, tel, and password and not on type Number, selectionStart and selectionEnd is out of option. I cannot change the type of the text box to text from number too.

Every commented code on placeCursorfunction are tried options with no luck on fixing the issue.

Please help place cursor on the text box when tab is pressed from text box Number1 to Number2 once it just has numerical digits.

Update

Getting

Uncaught RangeError: Maximum call stack size exceeded

On every .focus(). This is the problem which keeps the cursor not on the focused input text box. Try-Catch ignores the error, but does not places the cursor on the input textbox.Can someone help fix it?

Vikram Mohan
  • 39
  • 3
  • 15
  • Possible duplicate of [Setting focus to a textbox when a function is called](https://stackoverflow.com/questions/9543967/setting-focus-to-a-textbox-when-a-function-is-called) – Synapsis Jun 27 '19 at 07:24
  • @Synapsis : No I don’t think it’s a duplicate as .focus() is “Not” placing the cursor in the text box is my problem. And no, there is no uppercase confusion too. – Vikram Mohan Jun 27 '19 at 07:31
  • The TAB button is working for me on both google and IE and it also allows me to write. If it doesn't work for you just add the focus function so that when you insert a value in the first input field it will automatically focus the second one without pressing TAB If you want to check if your code works add it to this online editor: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default – Synapsis Jun 27 '19 at 09:38
  • @Synapsis tab works on the w3school tryit! But the tab was not working on the actual code, as it’s a large js with more validations on the form. – Vikram Mohan Jun 27 '19 at 09:51
  • Can you show me the part of code where you call the function? – Synapsis Jun 27 '19 at 10:00
  • @Synapsis the function “placeCursor()” is called from “focusInNumber()” and “focusOutNumber()” after validation, which is called by “.focusin()” and “.focusout()” jquery function. – Vikram Mohan Jun 27 '19 at 10:06
  • I saw that, but do you have a form calling these functions? What is calling these function in the HTML? Can I check that part of the code? – Synapsis Jun 27 '19 at 10:11
  • @Synapsis No html is plain HTML, no function calls there. I made the call on .focusin() jquery on the textbox. That is the function is called when the textbox is clicked or focused and it’s called when the textbox looses its focus or clicked elsewhere. – Vikram Mohan Jun 27 '19 at 10:13
  • I don't know how to help you then, sorry. – Synapsis Jun 27 '19 at 10:44

3 Answers3

0

<input type=“number” class=“reqField” id=“number1” placeholder=“Enter Number only/>
<input type=“number” class=“reqField” id=“number2” placeholder=“Enter Number only/>

You can try calling a function after you insert the value so that the cursor will focus on the next textbox.

function setFocus() {
    document.getElementById("myTextbox").focus();
}
Synapsis
  • 667
  • 1
  • 5
  • 19
  • What do you mean by “After you insert the value”?, do you mean on focusout() or after validation in focusOutNumber() function? I tried the same with placeCursor() function, “.focus()” does not place cursor in the text box. Please help. – Vikram Mohan Jun 27 '19 at 07:27
  • @vikrammohan It's strange that the "Tab" isn't working, try to use tab on the snippet I added to my answer and let me know if the "Tab" button allows you to write in the next box – Synapsis Jun 27 '19 at 09:33
  • What IE version are you using? same question for chrome... Also, try to run the code without any script and check if it works – Synapsis Jun 27 '19 at 09:39
  • IE version 9 and Chrome version 75.0.3770.100. Without the script file, the code won’t work, this validation and focus are a tiny part of whole form. – Vikram Mohan Jun 27 '19 at 09:50
0

Try to use the following code:

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script type="text/javascript">
    var numberReturn = 0;
    function focusInNumber(id) {
        var thisID = id;
        var nextID = id + 1;
        var preID = id - 1;
        $("#number" + thisID).prop("disabled", false);
    }
    function focusOutNumber(id) {
        var thisID = id;
        var nextID = id + 1;
        var preID = id - 1;
        var value = $("#number" + thisID).val();
        var regex = new RegExp(/^\d*$/);
        var regex1 = new RegExp(/^.*[\+\-\.].*/);
        var l = $("#number" + thisID).val().length;
        if (!value.match(regex)) {
            alert("Just enter numerical digits");
            $("#number" + thisID).val("");
            placeCursor($("#number" + thisID));
        } else {
            if (l <= 0) {
                alert("This field cannot be empty");
                placeCursor($("#number" + thisID));
            } else {
                if (value.match(regex)) {
                    console.log(nextID);
                    placeCursor($("#number" + nextID));
                    console.log("validate success");
                }
            }
        }
    }
    function placeCursor(id) {
        console.log(id);
        //$(id).focus();
        id.focus();
        var tmp = id.val();
        id.val("");
        id.val(tmp);
    }
    $(document).ready(function () {
        $("#number1").focusin(function () {
            focusInNumber(1);
        });
        $("#number1").focusout(function () {
            focusOutNumber(1);
        });
    });
</script>

The result as below (when press the tab button or click outside the first textbox, it will focus on the second textbox):

enter image description here

If still not working, try to create a new page and test above code, if still not working, perhaps the issue is related to the Browser, try to clear the browser cache and history or try to reset the browser setting.

Community
  • 1
  • 1
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • This works when created as separate HTML not when inscripted into original code...☹️ – Vikram Mohan Jun 28 '19 at 07:53
  • whether your page contains other javascript code, please try to debug your JavaScript code, perhaps there is somewhere to prevent this behavior. – Zhi Lv Jun 28 '19 at 14:57
  • It looks like .focus() is causing a “Uncaught RangeError: Maximum call stack size exceeded”. With try and catch I was able to ignore it, but .focus() still not working. ☹️ – Vikram Mohan Jul 01 '19 at 07:41
  • Updated the Question too. Please help fix it. – Vikram Mohan Jul 01 '19 at 07:49
  • About the "Maximum call stack size exceeded" error, I suggest you could try to refer [this thread](https://stackoverflow.com/questions/6095530/maximum-call-stack-size-exceeded-error) and [this article](https://codeburst.io/uncaught-rangeerror-maximum-call-stack-in-javascript-f1817760c4bf), whether you are using recursive or meet the out of Range problem. You could try to add some console.log method in the JavaScript function and key progress, then using F12 developer tools to trace the progress and debug your code. – Zhi Lv Jul 01 '19 at 08:59
  • I found the issue and fixed it. will answer it! – Vikram Mohan Jul 12 '19 at 04:47
-1

The stack size exceeded issue is caused by a global .focus()on input tag, that highlights the label of the input tag.

After deep digging found a solution to avoid the stack size exceeded issue and place cursor on the input area on tab.

Function placeCursor(id) {
 id.focus(function(e) {
  e.stopPropagation();
  e.cancelBubble();
  id.caretToEnd();
  });
  id.focus();
}

Adding .stopPropagation() and .cancelBubble() stopped going in loop for input tag .focus() and placeCursor() function’s .focus()

.caretToEnd() is from jquery.caret.js library

Also used setTimeout() on all placeCursor() call

var q1 = setTimeout(function() {
 placeCursor($(“#number” + thisID));
},100);

This worked like a charm.

Vikram Mohan
  • 39
  • 3
  • 15