Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Focus next input once reaching maxlength value

var container = document.getElementsByClassName("container")[0];
container.onkeyup = function(e) {
    var target = e.srcElement || e.target;
    var maxLength = parseInt(target.attributes["maxlength"].value, 10);
    var myLength = target.value.length;
    if (myLength >= maxLength) {
        var next = target;
        while (next = next.nextElementSibling) {
            if (next == null)
                break;
            if (next.tagName.toLowerCase() === "input") {
                next.focus();
                break;
            }
        }
    }
    // Move to previous field if empty (user pressed backspace)
    else if (myLength === 0) {
        var previous = target;
        while (previous = previous.previousElementSibling) {
            if (previous == null)
                break;
            if (previous.tagName.toLowerCase() === "input") {
                previous.focus();
                break;
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to add a key to every html tag in a list react 
Javascript :: jquery crud table example 
Javascript :: npm init step by step 
Javascript :: typeorm sqlite Using async/await syntax 
Javascript :: tekenaja 
Javascript :: automatic color change 
Javascript :: how to use sort with tiebreak in js 
Javascript :: on number copy pase formate in reactjs 
Javascript :: yarn redux devtool 
Javascript :: angular crud rest api medium 
Javascript :: setstate too slow 
Javascript :: angular optional attribute binding 
Javascript :: Example of Nullish coalescing assignment operator in es12 
Javascript :: Constant declaration in ES6 
Javascript :: angular material primary lighter 
Javascript :: import * as stringFunctions from "./string_functions.js"; // add code above this line stringFunctions.uppercaseString("hello"); stringFunctions.lowercaseString("WORLD!"); 
Javascript :: image support in node js chat app 
Javascript :: react native red Oval bubble 
Javascript :: filter array and get index of num 
Javascript :: loop through async javascript -5 
Javascript :: vue ignore not used error 
Javascript :: create trigger to run every minute in apps script 
Javascript :: javascript error fix 
Javascript :: on change swich 
Javascript :: return multiple native element react native 
Javascript :: add remove to array vue js 
Javascript :: sum the two first minimum numbers in an array 
Javascript :: how to wait for dom in javascript 
Javascript :: use only dispatch from useContext 
Javascript :: convert jquery hide function to pure javascript code 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =