I'm trying something easy, a typewriting effect:
var i = 0;
var txt = "eco eco"
var speed = 1;
function typeWriter(idtxt) {
if (i < txt.length) {
document.getElementById(idtxt).innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
};
<button class="remo-btn" onclick="typeWriter('demo')">
<p id="demo"></p>
When I hardcode the id, the code works as intended:
document.getElementById("demo").innerHTML += txt.charAt(i);
But when I pass the id like this:
document.getElementById(idtxt).innerHTML += txt.charAt(i);
It only returns the first letter, in this case, "e" (from echo).
Can somebody help me?