I have this code which uses a typewriter animation to replace the placeholder text in a search bar: https://codepen.io/anon/pen/vQmRjM
////////////////////////////
// Twitter: @mikedevelops
////////////////////////////
// your custome placeholder goes here!
var typetext = ["Text 1", "Text 2", "Text 3"];
var typetext = typetext[Math.floor(Math.random() * typetext.length)];
var searchBar = $('#search');
// placeholder loop counter
var phCount = 0;
// function to return random number between
// with min/max range
function randDelay(min, max) {
return Math.floor(Math.random() * (max-min+1)+min);
}
// function to print placeholder text in a
// 'typing' effect
function printLetter(string, el) {
// split string into character separated array
var arr = string.split(''),
input = el,
// store full placeholder
origString = string,
// get current placeholder value
curPlace = $(input).attr("placeholder"),
// append next letter to current placeholder
placeholder = curPlace + arr[phCount];
setTimeout(function(){
// print placeholder text
$(input).attr("placeholder", placeholder);
// increase loop count
phCount++;
// run loop until placeholder is fully printed
if (phCount < arr.length) {
printLetter(origString, input);
}
// use random speed to simulate
// 'human' typing
}, randDelay(50, 90));
}
// function to init animation
function placeholder() {
$(searchBar).attr("placeholder", "");
printLetter(typetext, searchBar);
}
window.setInterval(function(){
placeholder();
}, 3000);
I added a setInterval to loop through the different placeholder text every few seconds, however after the first iteration it just starts showing undefined.
Any ideas where I have went wrong?