0

I am trying to add line breaks in a for-loop in a stand alone .js file, linked to a HTML.

I have my variable set to an array with 3 objects inside. I want my for-loop to run once, then print the values on a new line in my browser using document.write. However, when I run my loop, all 3 objects are displayed in 1 single line instead of an actual list. I have tried using <br> & \n to add line breaks, but when I do, the text in my browser disappears. What am I doing wrong here?

let pokemonList = [{
    name: 'caterpie ',
    height: 0.3,
    types: ['bug', 'electric']
  },
  {
    name: 'sandslash ',
    height: 1,
    type: ['ground']
  },
  {
    name: 'meowth ',
    height: 0.4,
    type: ['normal']
  },
]

for (let i = 0; i < pokemonList.length; i++) {
  document.write(pokemonList[i].name + 'height: ' + pokemonList[i].height);
}
SuperDJ
  • 6,224
  • 7
  • 36
  • 62
lottofeet
  • 3
  • 2
  • I'm thinking `document` refers to an html document. – user1599011 May 19 '21 at 18:07
  • `
    ` should work. What was your code where it didn't?
    – VLAZ May 19 '21 at 18:09
  • maybe thats the problem, that Im not putting br in the correct spot. I've tried adding it in a number of locations and have not been successful. – lottofeet May 19 '21 at 18:11
  • I don't believe `document.write` is recommended any more. Instead, create a element (ex: `document.createElement('div')` ) and add it to some parent container. [Is document.write actually deprecated?](https://stackoverflow.com/q/12574098) – Johnny Mopp May 19 '21 at 18:20
  • Thank you all so much for the quick help & suggestions! I REALLY appreciate yall! – lottofeet May 19 '21 at 18:29

4 Answers4

1

Using, document.write just writes plain strings to your html code.

Wrap your item inside a div and since divs are block elements, they will automatically go to the next line.

Run the below snippet.

let pokemonList = [{
    name: 'caterpie ',
    height: 0.3,
    types: ['bug', 'electric']
  },
  {
    name: 'sandslash ',
    height: 1,
    type: ['ground']
  },
  {
    name: 'meowth ',
    height: 0.4,
    type: ['normal']
  },
]

for (let i = 0; i < pokemonList.length; i++) {
  document.write("<div>" + pokemonList[i].name + 'height: ' + pokemonList[i].height + " </div>");
}
Nisanth Reddy
  • 3,483
  • 1
  • 4
  • 22
0

You could use template literals to make it easier to read.

let pokemonList = [{
    name: 'caterpie ',
    height: 0.3,
    types: ['bug', 'electric']
  },
  {
    name: 'sandslash ',
    height: 1,
    type: ['ground']
  },
  {
    name: 'meowth ',
    height: 0.4,
    type: ['normal']
  },
]

for (let i = 0; i < pokemonList.length; i++) {
  document.write(`${pokemonList[i].name} height: ${pokemonList[i].height}<br/>`);
}
SuperDJ
  • 6,224
  • 7
  • 36
  • 62
0

you will need to add a few more line of code inside that for loop

//Step 1
const testElement = document.querySelector('div');
// Here you select the PokenmonList as your parent using the querySelector 

//Step 2
const lineBreak = document.createElement('br'); //Create a new BR element 

//Step 3
testElement.appendChild(lineBreak);
//then add the newly created br element to the pokenmonlist

Since I am unsure how your HTML looks like i am giving you a generalized way to append the child to your dynamic html

0

Do it like this:

    let pokemonList = [{
           name: 'caterpie ',
           height: 0.3,
           types: ['bug', 'electric']
         },
         {
           name: 'sandslash ',
           height: 1,
           type: ['ground']
         },
         {
           name: 'meowth ',
           height: 0.4,
           type: ['normal']
         },
       ]

       for (let i = 0; i < pokemonList.length; i++) {
         document.write(pokemonList[i].name + 'height: ' + pokemonList[i].height);
         document.write('<br/>');
       }
Ali Tabandeh
  • 66
  • 1
  • 3