I have to create the following type of HTML structure:
<div id="dcontent">
<ul class="thm">
<li><a href="#"><img src="img/theme1.jpg" id="t1" border="none"/></a></li>
<li><a href="#"><img src="img/theme2.jpg" id="t2" border="none"/></a></li>
<li><a href="#"><img src="img/theme3.jpg" id="t3" border="none"/></a></li>
I need to create 18 nested images this way so I use the following Javascript loop:
for (var count = 1; count < 19; count++) {
newLi = document.createElement('li');
newLi.setAttribute('id', 'l' + count);
newLink = document.createElement('a');
newLink.setAttribute('href', '#');
newLink.setAttribute('id', 'a' + count);
$(newLink).appendTo('#l' + count);
newImg = document.createElement('img');
newImg.setAttribute('src', thumbPath + count + '.jpg');
newImg.setAttribute('id', 't' + count);
newImg.setAttribute('border', 'none');
$(newImg).appendTo('#a' + count);
$(newLi).appendTo('.thm');
}
The code outputs only li nested in ul which is nested in div. Is it better to use jQuery because I read that document.createElement (pure Javascript) is faster method. Any help will be greatly appreciated.
1 Answer