[thelist] Simple JavaScript not working head scratcher

fstorr fffrancis at fstorr.demon.co.uk
Fri Mar 3 14:51:44 CST 2006

Hi all

This should be simple, but for some reason I can't work out why this  
isn't working.  This is what I've got:

<li>Quarter 1</li>
<li>Quarter 2</li>

And this is what I want:

<li><a href="#">Quarter 1</a></li>
<li><a href="#">Quarter 2</a></li>

Before anyone says anything, yes I know using JS to insert links is  
bad - this whole thing started off as a "I'll just write this script  
for a couple of quick development tasks to show proof of concept" and  
now it's just something that I can't get working.  In the actual  
document everything works fine except the inserting bit - I've tried  
using appendChild, firstChild.insertBefore and just about everything  
else, but it just won't play.  Here's what I've got:

<script type="text/javascript">
function alertnone()
    var content = document.getElementById("content");
    var lists = content.getElementsByTagName("ul");
    var items = lists[0].getElementsByTagName("li");
    for (var i=0;i<items.length;i++)
        var a = document.createElement("a");
        a.setAttribute("href", "#");
        items[i].lastChild.insertBefore(a, items[i]);

window.onload = alertnone;


<li>Quarter 1 link</li>
<li>Quarter 2 link</li>
<li>Quarter 3 link</li>



More information about the thelist mailing list