I am trying to dynamically create child elements and add a background image before appending it to the parent node. Everything is working except for the image files actually appearing. When you inspect the element in Mozilla, it reveals that the li elements have all been created and attached, and even contain the "style" attribute. I am certain it is my reference to the image array that I am not calling correctly...can someone please help me figure out how to use the array to reference the new background image?
external js file:
Code:
function placeThumbs()
{
var images = ["a.gif", "b.gif", "c.gif"];
var jgal = document.getElementById("gallery");
var newLi;
for (i=0; i<images.length; i++)
{
newLi = document.createElement("li");
jgal.appendChild(newLi);
newLi.id = i;
newLi.style.backgroundImage = images[i];
}
}
This is setting an id to an integer. If I remember correctly, ids/names cannot begin with anything but an underscore or a letter, and cannot be just integers. I think there are also rules about special characters, but I'm not recalling it - I avoid using them, anyway.
There could be something else, but fix that, first, and then see if it's still not working.
Problem solved, but your solution leads me to another question I hope you can answer for me. Can you explain to me what the following syntax actually does, so I can know when to use it in the future?
Code:
'url(' + images[i] + ')'
@wolfshade
I'm sure you're right about rules for ids. I was trying a couple of different methods to assign style attributes and accidentally left that snippet in the code.
Bookmarks