-1

In this website, if the letter entered is a new line, a new line would be returned, I have this like as follows

if (character === '\n'){
   element = document.createElement('br');

I tried adding var before, or even changing the single quotations to double, what is wrong with it?

Thank you in advance

emmy
  • 57
  • 8
  • Please share your whole code and some more details. You cannot tell what `character` or `element` are. Also what means *In this website*? – Behemoth Jul 19 '23 at 08:28
  • 1
    You can use `document.write("\n");` that will fix the problem – Umut Yalçın Jul 19 '23 at 08:30
  • 2
    Fix what problem @UmutYalçın? `document.write()` usually [causes more problems than it solves](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – Ivar Jul 19 '23 at 08:33
  • im sorry... @mplungjan i gave up and did not want any more time wasted on it... im sorry if i wasted your time on the question i asked... – emmy Jul 21 '23 at 10:00
  • But why give up? If I understand correctly, it was just a matter of switching the newline code to before the image i.e. change `return \`${img}${(i>0 && i%lineLength === 0) ? newLine : ''}\`;` to `return \`${(i>0 && i%lineLength === 0) ? newLine : ''}${img}\`;` – mplungjan Jul 21 '23 at 10:05
  • In any case, next time please tell the answerers that you are no longer interested in solving the issue before deleting. – mplungjan Jul 21 '23 at 10:06

1 Answers1

0

You need to then append that element with document.appendChild(element) https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

It looks like you're doing this inside a loop, so either create the HTML string all as one (as in the below snippet) or add the element like so:

   if (character === '\n'){
       element = document.createElement('br');
       document.appendChild(element)

// string to show on page
const stringToShow = `something
on a new
line`;


// create element and content
const divElement = document.createElement("div")
const content = stringToShow.split("\n").join("<br />")

// add content as HTML
divElement.innerHTML = content;
// append child to body, or any element for that matter
const elementToAppendTo = document.body
elementToAppendTo.appendChild(divElement)


// if (character === '\n'){
//    element = document.createElement('br');
//    document.appendChild(element)
<body></body>
Harrison
  • 1,654
  • 6
  • 11
  • 19