0

I want to create a table using Javascript , what should I choose?

I am having two options, but don't know what to choose:

  1. Creating a table element
  2. Writing document.write("<table ><tr>..........</tr></table>");

Can anybody give me a valid reason for doing one thing .

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
user2889070
  • 255
  • 1
  • 2
  • 6

3 Answers3

0

directly you can create by table tag n elements

0

First read why document.write is bad practice?

I guess you can create element rather than using document.write

Or see this approach:

<div id="myTable"></div>

Java script:

var tableDiv=document.getElementById("myTable")

var tableContent=""
tableContent+="<table><tr><td></td></table>";

tableDiv.innerHTML=tableContent;
Community
  • 1
  • 1
Shoaib Chikate
  • 7,697
  • 11
  • 41
  • 67
0

I think first choice is better.....try to visualize what you 'll end up doing in each option.

First option you create you dom elements by yourself. Think of it like writing objects. You have object and you have methods for it that you can use to add style or properties to it in a smooth way(maybe longer but nice).You can use JS functions to make your markup dynamic
Second option It's just bunch of strings which are actually your html markup but as strings. And you 'll end up with static markup

Now imagine you need to add custom property to the second row(like data-xxxx):
First option all you need is to find your second row object and use the provided functions to add whatever you want.
Second option I bet that you 'll take the html markup to some kind of editor(e.g; http://jsfiddle.net) then format the markup to be nice and readable. then finally you search for the second row and add your custom properties

Another benefit with first option is if your are using JS libraries like great jQuery it going to be a piece of cake.

ebram khalil
  • 8,071
  • 7
  • 39
  • 55