Skip to content

Instantly share code, notes, and snippets.

@daksamedia
Created October 22, 2018 03:24
Show Gist options
  • Save daksamedia/b255a3c37a9a677d457a672ee40947a8 to your computer and use it in GitHub Desktop.
Save daksamedia/b255a3c37a9a677d457a672ee40947a8 to your computer and use it in GitHub Desktop.
otp_input
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