How to do (javascript typewriter effect)? Step by step | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to do (javascript typewriter effect)? Step by step

8th Jan 2019, 8:38 PM
hanan alhinai
hanan alhinai - avatar
10 Answers
+ 4
You need to apply following knowledges: - getting DOM elements - setInterval() and clearInterval() - for loop - length property Open a blank web code and tag me in there. I'll teach you step by step
9th Jan 2019, 4:14 AM
Gordon
Gordon - avatar
+ 4
This is an interesting and easy thing to do with js. Here are the steps. 1.Store the text that to be in the typing effect in a variable. var txt = "Cat ipsum dolor sit amet..." 2.Make an element in html to display the characters. var elem = document.createElement("p"); document.appendChild(elem); 3.Loop through each character in the string. for(let c of txt.split("")) {...} 4. Print each element at a specific time period inside the loop. for... { setTimeout(()=> { elem.innerHTML += c; },500) //types every 0.5sec. } And then enjoy the effect EDIT: Above version is not working for some reasons. Try this instead. var txt = "12345"; var i=0; window.onload = () => { let interval = setInterval(()=>{ document.querySelector("p").innerHTML += txt.charAt(i++); if(i==txt.length) {clearInterval(interval)} },500); }
10th Jan 2019, 1:30 PM
Seniru
Seniru - avatar
+ 3
Gordon I checked it and its not working. Thanks for letting me know.
10th Jan 2019, 3:36 PM
Seniru
Seniru - avatar
+ 2
Gordon nope why? is there any error?
10th Jan 2019, 3:02 PM
Seniru
Seniru - avatar
+ 2
The timeout time, if for every character, it's the same (500ms),what would happen?
10th Jan 2019, 3:04 PM
Gordon
Gordon - avatar
+ 1
Dear friend I have 2 easy options for you 1. Use Typed.js JavaScript Library https://mattboldt.com/demos/typed-js/ 2. Or follow this link to do it yourself. https://youtu.be/POX3dT-pB4E I hope this helps.
8th Jan 2019, 10:20 PM
africana
africana - avatar
+ 1
Thank you
9th Jan 2019, 5:45 AM
hanan alhinai
hanan alhinai - avatar
+ 1
Seniru Pasan Have you tried your point 4 in code playground?
10th Jan 2019, 2:38 PM
Gordon
Gordon - avatar
+ 1
Another resource: https://css-tricks.com/snippets/css/typewriter-effect/ Three of the seven examples in the link were able to simulate the effect with just HTML and CSS. The remaining four use JavaScript.
10th Jan 2019, 2:57 PM
Janning⭐
Janning⭐ - avatar
+ 1
Also, please see prior post: https://www.sololearn.com/Discuss/479514/?ref=app Many good examples and explanations there too.
10th Jan 2019, 3:01 PM
Janning⭐
Janning⭐ - avatar