JavaScript Javascript typewriter() on two lines

So I'm creating a basic website, and I used the typewriter function from w3schools:

https://www.w3schools.com/howto/howto_js_typewriter.asp

My problem is that I want to display the text on 2 lines, and I'm not entirely sure how to do that. Anything helps!

Thanks

var i = 0;
var txt = 'Lorem ipsum typing effect!'; /* The text */
var speed = 50; /* The speed/duration of the effect in milliseconds */

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<span id='demo'></span>
Answer:1

You would want to get a <br/> in there.

You could do something like this:

var i = 0;
var txt = `Lorem ipsum dummy \n text blabla.`;
var speed = 50;

function typeWriter() {
  if (i < txt.length) {
    let text = txt.charAt(i);
    document.getElementById("demo").innerHTML += text=== "\n" ? "<br/>": text;
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<span id='demo'></span>
Answer:2

You could give the areas where you would like to append your text to a class such as typer, so that you can target multiple elements. Then you can use getElementsByClassName to get all the elements with the class typer. Using .forEach you can then loop over all your elements (in this case divs) and append the appropriate character to them.

See working example below:

var i = 0;
var txt = 'Lorem ipsum typing effect!'; /* The text */
var speed = 50; /* The speed/duration of the effect in milliseconds */

function typeWriter() {
  if (i < txt.length) {
    [...document.getElementsByClassName("typer")].forEach(e => {
      e.innerHTML += txt.charAt(i);
    })
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<div class="typer"></div>

<div class="typer"></div>
Answer:3

You can use <br/> in there.

var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;
var firstLineMaxChars = 10

function typeWriter() {
  if (i < txt.length) {
    var char = txt.charAt(i)

    if (i == firstLineMaxChars) {
      char += '</br>'
    }

    document.getElementById("demo").innerHTML += char;
    i++;
    setTimeout(typeWriter, speed);
  }
}
<h1>Typewriter</h1>

<button onclick="typeWriter()">Click me</button>

<p id="demo"></p>
Answer:4

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

  1. when to use document.write
  2. when would you use document.write()

After some research the following suggestion by Mr. Evan You was found: https://github.com/vuejs/vue/issues/7349#issuecomment-354937350 So without any hesitation I gave it a try: Component template ...

After some research the following suggestion by Mr. Evan You was found: https://github.com/vuejs/vue/issues/7349#issuecomment-354937350 So without any hesitation I gave it a try: Component template ...

Currently, I use PrestaShop, and I wanted to change the width of search box, but, when I change the width of search box to 650px, on desktop it's ok, but on mobile the search box is very long — not ...

Currently, I use PrestaShop, and I wanted to change the width of search box, but, when I change the width of search box to 650px, on desktop it's ok, but on mobile the search box is very long — not ...

Say you have the following array: const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a']; How would you change this array so that all the "b" items get grouped together, until you hit another "a". ...

Say you have the following array: const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a']; How would you change this array so that all the "b" items get grouped together, until you hit another "a". ...