Created
October 22, 2018 03:24
-
-
Save daksamedia/b255a3c37a9a677d457a672ee40947a8 to your computer and use it in GitHub Desktop.
otp_input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = document.getElementById('partitioned'); | |
obj.addEventListener("keydown", stopCarret); | |
obj.addEventListener("keyup", stopCarret); | |
function stopCarret() { | |
if (obj.value.length > 3){ | |
setCaretPosition(obj, 3); | |
} | |
} | |
function setCaretPosition(elem, caretPos) { | |
if(elem != null) { | |
if(elem.createTextRange) { | |
var range = elem.createTextRange(); | |
range.move('character', caretPos); | |
range.select(); | |
} | |
else { | |
if(elem.selectionStart) { | |
elem.focus(); | |
elem.setSelectionRange(caretPos, caretPos); | |
} | |
else | |
elem.focus(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment