javascript - Tell amounts of a table column and put that amount as the top collspan -


is possible check number of columns in table exists , assign top of table colspan?

my code

document.write('<table border="1" cellspacing="1" cellpadding="5">') document.write('<thead><tr><th colspan="2"> head grid </th></tr></thead>')  for(i = 0; < 13; i++){    document.write('<tr>')    document.write('<td>row ' + + ', column 0</td>')    document.write('<td>row ' + + ', column 1</td>')    document.write('<td>row ' + + ', column 2</td>')    document.write('</tr>') }  document.write('</table>') 

is possible?

assuming id on table, e.g.

<table id="mytable" border="1" cellspacing="1" cellpadding="5">   <thead><tr><th colspan="2"> head grid </th></tr></thead>   <tr>     <td>row 0, column 0</td>     <td>row 0, column 1</td>     <td>row 0, column 2</td>   </tr>   <tr>     <td>row 1, column 0</td>     <td>row 2, column 1</td>     <td>row 3, column 2</td>   </tr>   <!-- etc. --> </table> 

and assuming jquery (since it's in tags):

var maxcols = 1;  // @ each row in table // $('#mytable tr').each(   function() {     // grab count of columns in row     //     var cols = $(this).children('td').length;      if (cols > maxcols)       maxcols = cols;   } );  // set `colspan` on our header, assuming single `th` in // example // $('#mytable th').attr('colspan', maxcols); 

example: http://codepen.io/paulroub/pen/boksc


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -