Put each element of an array, in a cell of a table created by Javascript -
i've created table using javascript. however, each of cells (td
) in table display each element in array, in order of elements in array. array words in it:
var myarray=['anime','demon','black','death','beast','tokyo','manga','titan','ghoul'];
here javascript code far:
function display_array(myarray){ var body = document.body, tbl = document.createelement('table'); tbl.style.width = '100px'; tbl.style.border = "1px solid black"; var arraylength = myarray.length; var sqroot=math.sqrt(arraylength); for(var = 0; < sqroot; i++){ var tr = tbl.insertrow(); for(var j = 0; j < sqroot; j++){ if(i == sqroot && j == sqroot){ break; } else { var td = tr.insertcell(); for(var q = 0; q < arraylength; q++){ td.appendchild(document.createtextnode(myarray[q])); } td.style.border = "1px solid black"; if(i == 1 && j == 1){ td.setattribute('rowspan', '1'); } } } } body.appendchild(tbl); }
with code, table looks this:
____________________________________________________________________________________________________________________________________________ |animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul| -------------------------------------------------------------------------------------------------------------------------------------------- |animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul| -------------------------------------------------------------------------------------------------------------------------------------------- |animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul|animedemonblackdeathbeasttokyomangatitanghoul| --------------------------------------------------------------------------------------------------------------------------------------------
i table this:
___________________ |anime|demon|black| ------------------- |death|beast|tokyo| ------------------- |manga|titan|ghoul| -------------------
for(var q = 0; q < arraylength; q++){ td.appendchild(document.createtextnode(myarray[q])) }
this loops thru , create textnode each element. if want 1 element use myarray[index]
; in case index i * sqroot + j
.
td.appendchild(document.createtextnode(myarray[i * sqroot + j]))