Why do not you make a line break?

Why do not you make a line break?
0.0 0

#1

Hello, I’m trying to make a break in this chain, but it does not work either, or with “

If you can give me a clue, what am I doing wrong?
Thank you.

  if (userGuess === randomNumber) {
                lastResult.textContent = `Congratulations! You got it right! <br />You won in ${guessCount} attempts`
                lastResult.style.backgroundColor = 'green'
                lowOrHi.textContent = ''
                setGameOver()

#2

You are setting the text content as opposed to the actual HTML markup. Presumably, you’re actually seeing the <br />on the screen? To update the actual markup (what you probably want to include a line break), use lastResult.innerHTML instead of lastResult.textContent. Beware that if you use innerHTML when dealing with user-submitted content, you may be opening yourself to security vulnerabilites. For example, a user could type a script tag into a search field. If you use innerHTML, the script would run, and this can be a massive security vulnerability known as XSS. However, if you’re not dealing with user-submitted content, you can use innerHTML without worrying.

TLDR: You need to use innerHTML instead of textContent.

Hope this solves your issue!


#3

Yes, really what I needed was to make a line break.
The text is simply a notification to the user, saying that he won the game and how many attempts he got.
But that does not work, I do not know how I can get it …
Sorry for waiting so long to respond.
Thank you.
I forgot to say that the text I generate it in javascript code, not in the html


#4

It sounds like in your case, what you are doing is retrieving some data (e.g. how many attemps the user had), and you use that data to generate a notification message to show the user. In this case, you can use innerHTML without worrying about security vulnerabilites, as you would do something like this:

lastResult.innerHTML = `Congratulations! You got it right!  <br /> You won in ${guessCount} attempts`

In the above code, innerHTML is safe because you are controlling all the HTML tags that are inserted (in this case only <br /> tags are inserted), so you can be sure that there are no malicious <script /> tags being inserted. The only time you need to worry about using innerHTML is if you are literally just taking some content the user submitted and displaying it. In this case, I assume the guessCount variable is a number that you generate based on the user’s performance, and so it cannot contain HTML tags. So, using innerHTML is safe here, whereas if guessCount was something the user typed into an input, it would not be safe, because the user could for instance type <script>alert(You have been hacked)</script>, and the code would run, but in this case, you know that the variable is just a number.

I would suggest you just use innerHTML.

I hope I am understanding yojur question correctly.


#5

Thanks for your support, I corrected it in the following way:
lastResult.innerHTML =