I have a dynamic table that I want to attach <col>
elements with jQuery.
I have this:
var tds = jQuery("tr > td").length; // count how many tds
jQuery("#colgroup-compare > col").clone() // clone
.appendTo('#colgroup-compare'); // and append
Obviously this only appends 1 <col>
, I want to append (n) numbers. How would I do this?
I have the length, I have the clone ability, now how do I combine it?
With a loop :
You can't use
jQuery("#colgroup-compare > col").clone().appendTo('#colgroup-compare');
in your loop because that would append more cols at iterations > 0...This can be optimized :
EDIT : to count the
th
in your first row, you may do this :(this avoid counting on more than one row)
Demonstration
If you don't want deep clones, then you can avoid the manual iteration by passing the outerHTML of the element to an arrays
join()
method resulting in an HTMLString corresponding ton
number of elements as shown below:which you can append to any element you wish.
In your case you can do: