Skip to content

Instantly share code, notes, and snippets.

@jeremybenaim
Last active August 29, 2015 14:23
Show Gist options
  • Save jeremybenaim/471de5533f6eb6c7aeb1 to your computer and use it in GitHub Desktop.
Save jeremybenaim/471de5533f6eb6c7aeb1 to your computer and use it in GitHub Desktop.
Typewriting style pre-populate
/*
* Input populate with typerwriting emulation (sort of...)
* (e.g. for url like /lostpassword?email=xxx@yyy.zzz)
*/
var email = "xxx@yyy.zzz", // 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