Skip to content

Instantly share code, notes, and snippets.

@katakeynii
Last active July 19, 2018 11:24
Show Gist options
  • Save katakeynii/63a9183fb6ed9772c382dade5c5957ff to your computer and use it in GitHub Desktop.
Save katakeynii/63a9183fb6ed9772c382dade5c5957ff to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Challenge DOM Manipulation</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
const rootEl = document.getElementById('root');
const createInput = (limit, position, is_last)=>{
let input = document.createElement('input');
input.setAttribute('maxlength', limit);
input.setAttribute('class', `input-${position}`);
input.addEventListener('keyup', (e)=>{
if(e.target.value.length === limit){
let nextElement = input.parentNode.children[position+1];
if(is_last){
nextElement.removeAttribute('disabled')
}else{
nextElement.focus()
}
}
});
return input;
};
const createGroup = (inputs) =>{
let div = document.createElement('div');
for (var i = 0; i < inputs.length; i++) {
let is_last = i == (inputs.length - 1);
let input = createInput(inputs[i], i, is_last);
div.appendChild(input);
}
let button = document.createElement('button');
let textNode = document.createTextNode("Envoyer");
button.setAttribute("disabled", false);
button.setAttribute("id", "button-submit");
button.appendChild(textNode);
div.appendChild(button);
rootEl.appendChild(div);
}
createGroup([4, 2, 2, 4]);
createGroup([4, 2, 2]);
createGroup([4, 2, 2, 5, 3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment