1

I'm making an application where the JavaScript code is all in the head but I need to output the data into a table in the body.

I'm currently using the following code from the head which just writes to the body. But it's pretty sloppy, I'd prefer to have it in a table.

document.body.innerHTML = (document.body.innerHTML+'<br />'+place.name);

How would I do this?

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
dlofrodloh
  • 1,728
  • 3
  • 23
  • 44
  • Put an `id` attribute to your table, and refer to id using `document.getElementById('#your-id')`. – Rubens Mariuzzo Aug 28 '13 at 22:42
  • 2
    Do you want the script to create the table, or will the table be there to begin with? Do you want to create table rows and td elements on the fly, or will they be there to begin with? –  Aug 28 '13 at 22:43
  • as the first comment said but make sure to place your script after the table in you html. – Math chiller Aug 28 '13 at 22:43
  • If you need to create tables, I have a [pretty extensive answer about various methods to create HTML tables from arrays](http://stackoverflow.com/a/18139847/1074592) – Brigand Aug 28 '13 at 22:44
  • Just for reference, your example code could be shortened to: `document.body.innerHTML += ('
    '+place.name);`. You can create elements with document.createElement('div') [example for div, just put any tag name in there] `document.body.appendChild( document.createTextNode('Hello World') );` will just add the text Hello World to the doc. You can append a text node to a div, then append that div to the document (or some other element). Your example could be done with: `document.body.appendChild( document.createElement('br') ); document.body.appendChild( document.createTextNode(place.name) );`
    – enhzflep Aug 28 '13 at 23:05

0 Answers0