Last active
August 29, 2015 14:23
-
-
Save jeremybenaim/471de5533f6eb6c7aeb1 to your computer and use it in GitHub Desktop.
Typewriting style pre-populate
This file contains hidden or 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
/* | |
* Input populate with typerwriting emulation (sort of...) | |
* (e.g. for url like /[email protected]) | |
*/ | |
var email = "[email protected]", // e.g. get email from url query params | |
input = document.querySelector('input[name="email"]'), | |
splitted = email.split(''), | |
type; | |
function _preventDefault(e){ | |
e.preventDefault(); | |
} | |
input.addEventListener('keypress', _preventDefault); | |
input.addEventListener('focus', function(){ | |
input.removeEventListener('keypress', _preventDefault); | |
}); | |
type = window.setInterval(function(){ | |
if(!splitted.length) { | |
window.clearInterval(type); | |
input.focus(); | |
} else { | |
shifted = splitted.shift(); | |
input.value += shifted; | |
} | |
}, 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment