1

I am using this code which essentially types text onto the screen. I am unsure how to add a new line to the string which is being displayed.

I have already tried \n for those posting their answers. This does NOT work. A new line is not started in my HTML

Code:

var myString = "public class MyResume implements Resume{" +
    /*this is where I want the new line*/ "...." ;

var myArray = myString.split("");
var loopTimer;
function frameLooper() {
  if(myArray.length > 0) {
    document.getElementById("myTypingText").innerHTML += myArray.shift();
  } else {
    clearTimeout(loopTimer); 
    return false;
  }
  loopTimer = setTimeout('frameLooper()',70);
}
frameLooper();
<div id="myTypingText"></div>

2
  • 1
    var myString = 'public class MyResume implements Resume{\n'+ Commented Aug 30, 2016 at 2:22
  • I tried that already. It doesn't start a new line, but the '\n' does not appear in the text Commented Aug 30, 2016 at 2:23

5 Answers 5

4

You can also use <br>.Just like"your string.<br> new line"

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work either. The HTML isn't being recognized. It just displays "<br>" on the screen
2

Here's an overly simplistic approach with full code. Use a tilde ~ and then watch for it in your frameLooper to insert a
like this:

<html>
<body>
<div id="myTypingText"></div>
<script>
var myString = 'public class MyResume implements Resume{~....' ;

var myArray = myString.split("");
var loopTimer;
function frameLooper() {
if(myArray.length > 0) {
    var char = myArray.shift();
        if (char === '~')
    { document.getElementById("myTypingText").innerHTML += '<br/>'; }
    else
    { document.getElementById("myTypingText").innerHTML += char; }
} else {
    clearTimeout(loopTimer); 
            return false;
}
loopTimer = setTimeout('frameLooper()',70);
}
frameLooper();
</script>
</body>
</html>

Comments

2

Simply adding <br> to myString doesn't work because you're inserting each character at one time. When a character gets added with innerHTML, JavaScript encodes it:

$('element').innerHTML += "<";
> "string&lt;" 

If you did this for each character in <br>, you'd end up with

>"string&lt;br&lt;"

You need some way to tell your script to add the entire element when you reach a "break character". You could use an uncommon character like a pipe | or you could add a method which looks ahead to make sure that the next few characters don't spell out <br>.

3 Comments

Yeah, that makes a lot of sense. Thanks. Since it's splitting each character into an array, how would I be able to find the break statement? Or is there an easier way of doing this?
Use an uncommon character or teach your script to look ahead.
Can I add the uncommon character and then split the string based on that character. Then I could individually split each substring within its own characters?
1

To add string to a new line, you need the \n in your string. For example:

var string = 'This is the first line \nThis is the second line'

console.log(string)

This would output

This is the first line 
This is the second line

2 Comments

Nope, doesn't work. It doesn't start a new line, but the '\n' does not appear in the text
I know how to start a new line, its just that it isn't working in my HTML
0

Why don't you just append ul>li or p to your text, something like this:

document.getElementById("myTypingText").innerHTML += "<p>" + myArray.shift() "</p>";

or

document.getElementById("myTypingText").innerHTML += "<li>" + myArray.shift() "</li>";

with:

<ul id="myTypingText"></ul>

1 Comment

This just adds each character to a new paragraph lol. I want to format the string to look like an actual java program.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.