Question : createElement and appendChild to all matching elements

Hello,

What I would like to do is this: I want to create a new div and append it to all matching referenced elements.  I have searched far and wide for the answer but I can't seem to figure out a straightforward way to do this...most of the documentation I've read pertains to appending an element once, whereas I want to do it to every element with a matching id.  Here's my code so far:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
 <script type="text/javascript">
var newDiv;
var labels;
function addElement()
{
labels = document.getElementsByTagName("a"); 
 for( var i = 0; i < labels.length; i++ ) 
 { 
  if(labels[i].className == "linkclass") {
  newDiv = document.createElement("div");
  newDiv.id = "myContent";
  labels[i].appendChild(newDiv);
 
  }
 } 
}

There are multiple <a> classes in my document, which is why I want to target linkclass, and add the newDiv each time linkclass shows up in my document.  The above code only works for the first time linkclass shows up.   I tried something like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
 <script type="text/javascript">
var newDiv;
var labels;
function addElement()
{
labels = document.getElementsByTagName("a"); 
 for( var i = 0; i < labels.length; i++ ) 
 { 
  if(labels[i].className == "linkclass") {
  newDiv[i] = document.createElement("div");
  newDiv[i].id = "myContent";
  labels[i].appendChild(newDiv[i]);
 
  }
 } 
}

But this didn't work at all.  Perhaps I need jQuery for this, but I wouldn't know what the code would look like.  Any help on this would be greatly appreciated.

*Edit* I should probably mention that I want to place a Flash object into the divs after they have been rendered, which might be the source of my trouble.  Here's the script I put toward the bottom of the page.

1:
2:
3:
4:
5:
6:
7:
8:
9:
<script type="text/javascript">
addElement();
var flashvars = {};
var params = {
  wmode: "transparent"
};
var attributes = {};
    swfobject.embedSWF("flash.swf", "myContent", "85", "100", "8.0.0","expressInstall.swf", flashvars, params, attributes);
</script>


(And of course I have the swfobject.js linked earlier in my file).

Again it works in that the flash object is placed into the first linkclass element, but only once.  I'd like it placed in all of them.

Answer : createElement and appendChild to all matching elements

The answer depends on the STL container. Node based containers such as map and set do not invalidate existing contents when adding or deleting but containers such as vector and deque do.

My article on STL containers explains this and a lot more.

http://www.experts-exchange.com/Programming/Languages/CPP/A_2812-Which-STL-Container.html
Random Solutions  
 
programming4us programming4us