Skip to content

Instantly share code, notes, and snippets.

@jacky810124
Forked from anonymous/index.html
Created July 16, 2017 15:06
Show Gist options
  • Save jacky810124/7085ebd722df34d3f2be0fb08f2ff4d4 to your computer and use it in GitHub Desktop.
Save jacky810124/7085ebd722df34d3f2be0fb08f2ff4d4 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jiwadudidu
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
* {
margin: 0;
padding: 0;
}
.box {
float: left;
margin: 50px 10px;
position: relative;
width: 30px;
height: 30px;
border-bottom: 5px solid #fcc;
}
input {
border: 0;
position: absolute;
top: 0;
left: 0;
z-index:999;
width: 100%;
height: 100%;
text-align: center;
background: transparent;
opacity: 0;
}
input:focus {
outline: 0;
}
input:valid {
opacity: 1;
}
input:focus ~ .circle,
input:valid ~ .circle {
display: none;
}
.circle {
position: absolute;
top: 5px;
left: 5px;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fcc;
}
</style>
</head>
<body>
<div class="box">
<input type="text" maxlength="1" required="required">
<div class="circle"></div>
</div>
<div class="box">
<input type="text" maxlength="1" required="required">
<div class="circle"></div>
</div>
<div class="box">
<input type="text" maxlength="1" required="required">
<div class="circle"></div>
</div>
<div class="box">
<input type="text" maxlength="1" required="required">
<div class="circle"></div>
</div>
<div class="box">
<input type="text" maxlength="1" required="required">
<div class="circle"></div>
</div>
<script id="jsbin-javascript">
const nodes = document.querySelectorAll('.box>input')
Array
.from(nodes)
.map((item, index) => {
item.addEventListener('keyup', (event) => {
let target
if (event.code === 'Backspace' && index > 0) {
target = item.parentNode.previousElementSibling.children[0]
target.value = ''
target.focus()
} else if (item.value != null && item.value !== '' && index < 4) {
target = item.parentNode.nextElementSibling.children[0]
target.focus()
} else {}
})
})
</script>
<script id="jsbin-source-css" type="text/css">* {
margin: 0;
padding: 0;
}
.box {
float: left;
margin: 50px 10px;
position: relative;
width: 30px;
height: 30px;
border-bottom: 5px solid #fcc;
}
input {
border: 0;
position: absolute;
top: 0;
left: 0;
z-index:999;
width: 100%;
height: 100%;
text-align: center;
background: transparent;
opacity: 0;
}
input:focus {
outline: 0;
}
input:valid {
opacity: 1;
}
input:focus ~ .circle,
input:valid ~ .circle {
display: none;
}
.circle {
position: absolute;
top: 5px;
left: 5px;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fcc;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">const nodes = document.querySelectorAll('.box>input')
Array
.from(nodes)
.map((item, index) => {
item.addEventListener('keyup', (event) => {
let target
if (event.code === 'Backspace' && index > 0) {
target = item.parentNode.previousElementSibling.children[0]
target.value = ''
target.focus()
} else if (item.value != null && item.value !== '' && index < 4) {
target = item.parentNode.nextElementSibling.children[0]
target.focus()
} else {}
})
})
</script></body>
</html>
* {
margin: 0;
padding: 0;
}
.box {
float: left;
margin: 50px 10px;
position: relative;
width: 30px;
height: 30px;
border-bottom: 5px solid #fcc;
}
input {
border: 0;
position: absolute;
top: 0;
left: 0;
z-index:999;
width: 100%;
height: 100%;
text-align: center;
background: transparent;
opacity: 0;
}
input:focus {
outline: 0;
}
input:valid {
opacity: 1;
}
input:focus ~ .circle,
input:valid ~ .circle {
display: none;
}
.circle {
position: absolute;
top: 5px;
left: 5px;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fcc;
}
const nodes = document.querySelectorAll('.box>input')
Array
.from(nodes)
.map((item, index) => {
item.addEventListener('keyup', (event) => {
let target
if (event.code === 'Backspace' && index > 0) {
target = item.parentNode.previousElementSibling.children[0]
target.value = ''
target.focus()
} else if (item.value != null && item.value !== '' && index < 4) {
target = item.parentNode.nextElementSibling.children[0]
target.focus()
} else {}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment