Looping through an array of nodes and appending a child to each one only
appends to the last element
I want to loop through an array of nodes referring to SVG elements and
append a text element to each one but for some reason all of the text only
appears in the last SVG element in the array.
Here's the code
var svgs = document.getElementsByTagName('svg');
var moduleNames = ["1", "2", "3", "4", "5"];
var textEl = document.createElementNS(svgns, 'text');
var i = 1;
while(i < moduleNames.length) {
textNode = document.createTextNode( moduleNames[i] );
textEl.appendChild(textNode);
svgs[i].appendChild(textEl);
i++;
}
Oh, and before I do this I already know the number of SVG elements will be
the same length as the moduleNames array.
Thanks!
No comments:
Post a Comment