42

I am trying to create a table with images in first cell and information about the pic in second cell.

I need to add different information in one cell, like that:

cellTwo.innerHTML = arr_title[element] + arr_tags[element];

Is it possible to add a "new line" there? I mean like that:

cellTwo.innerHTML = arr_title[element] + "/n" + arr_tags[element];
Gudron Swiss
  • 445
  • 1
  • 4
  • 6

3 Answers3

49

The simplest way is by adding a line break as html

cellTwo.innerHTML = arr_title[element] + "<br />" + arr_tags[element];

If you want your newlines to be treated literally, you could use the <pre> tag

cellTwo.innerHTML = 
    "<pre>" + arr_title[element] + "\n" + arr_tags[element] + "</pre>";
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
16

To round out your understanding:

Since it is html (innerHTML) it renders html and you can use any html you wish, so in this case simply add an good old fashioned <br>:

var test = document.getElementById('someElementId');
test.innerHTML = "The answer <br>to life, the universe, and everything...<br> is 42.";

If it were a string, such as in an alert box or text box etc. then /n would be correct:

alert('Never /n Forget your towel.'); 

Happy Coding!
   - $cr1ptN!nj@

Sergio
  • 28,539
  • 11
  • 85
  • 132
cbur
  • 169
  • 1
  • 3
-2

No, <br /> does not work in asp .net but you can instead write it like so

cellTwo.innerHTML = arr_title[element] + arr_tags[element]; arr_title[element] + "/n" + arr_tags[element];

Edit - alternative wrapped in code tags

cellTwo.innerHTML = arr_title[element] + arr_tags[element];
cellTwo.innerHTML += arr_title[element] + "/n" + arr_tags[element];

Semicolon ";" seems to act as line breaks Remember the "+=" to assign multiple values to the string

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Parnell
  • 21
  • 4
    `
    ` works perfectly fine in HTML generated from ASP.NET. `"/n"` doesn't mean a new line anywhere as far as I know. `\n` means a new line in a JavaScript string literal, but is treated like any other whitespace when converted to HTML. A semicolon does not act like a line break in JavaScript, a semicolon ends a statement. Sometimes a line break with cause a semicolon to be inserted automatically in JS. In short, absolutely everything about this answer is wrong (and mostly the exact opposite of correct)
    – Quentin Jul 16 '16 at 22:46