Home > database >  Line break after printing array's element
Line break after printing array's element

Time:10-21

So I have this code which checks if an element of array starts with letter Ա/ա, and prints it by adding into a paragraph. But I can not make every word start from a new line.

for (var i = 0; i < aremove.length; i  ) {
  aremove = [...new Set(aremove)].sort()
  if (/^[Աա]/.test(aremove[i])) {
    var word = document.createTextNode(aremove[i])
    document.getElementById("p").appendChild(word)
  }
}

If I add "<br>", it's not breaking the line but simply adds <br> to words. If I add .join("<br>") it returns "TypeError: aremove[i].join is not a function" or "word.join is not a function" and so on...

CodePudding user response:

By help of Teemu's comment

for (i = 0; i < aremove.length; i  ) {
  aremove = [...new Set(aremove)].sort()
  if (/^[Աա]/.test(aremove[i])) {
    word = document.createTextNode(aremove[i])
    linebreak = document.createElement("br")
    document.getElementById("girq").appendChild(word)
    document.getElementById("girq").appendChild(linebreak)
  }
}

I have created a "br" element, and as I can't append an element and text node at once, I appended it separately.

  • Related