Sto generando un menu per una pagina xhtml a partire da un file xml usando ajax.
Il file xml viene letto senza problemi e viene chiamata la funzione processXML che genera una lista di link. Fino a qua tutto è tranquillo. Il problema è questo: ho aggiunto un attributo ID al tag LI (viene gestito dalla varibile LABELLI) che semplicemente è un numero sequenziale, dopodichè ho bisogno di attaccare un evento di tipo ONCLICK per richiamare una mia funzione per ogni link.
La funzione in questione è LOADCONTENT. L'errore che trovo è che la funzione viene sempre chiamata con l'ultimo ID generato, e non con il corrispondente assegnato al tag LI.
Cosa devo correggere?

function processXML(xmlHttp,div)
{
if(window.ActiveXObject)
{
// Internet Explorer
// get DOM from a requested page
var response = new ActiveXObject("Microsoft.XMLDOM");
response.loadXML(xmlHttp.responseText);
}
else
{
// Firefox, Opera 8.0+, Safari
// get DOM from a requested page
var response = xmlHttp.responseXML;
}
var itemlist = response.getElementsByTagName("nome");
var numitems = itemlist.length;

var labelul = document.createElement("ul");
for(var i=0; i<numitems; i++)
{
var labelli = document.createElement("li");


var pid = document.createAttribute("id");
pid.nodeValue = i;
labelli.setAttributeNode(pid);

var labela = document.createElement("a");

labela.href = "#";


if(window.addEventListener)
{
// Firefox, Opera 8.0+, Safari
labela.addEventListener('click', function(){loadContent(response,labelli.getAttribu te("id"));}, false);
}
else
{
// Internet Explorer
labela.attachEvent('onclick', function(){loadContent(response,labelli.getAttribu te("id"));});
}

if(document.all)
{
// Internet Explorer
labela.innerText = itemlist[i].firstChild.nodeValue;
}
else
{
// Firefox, Opera 8.0+, Safari
labela.textContent = itemlist[i].firstChild.nodeValue;
}

labelli.appendChild(labela);
labelul.appendChild(labelli);
}

document.getElementById(div).appendChild(labelul);

}


function loadContent(XMLdoc, pid)
{
var itemlist = XMLdoc.getElementsByTagName("nome");
var numitems = itemlist.length;
alert("pid :" + pid);

for(var i=0; i<numitems; i++)
{
alert("A: " + itemlist[i].firstChild.nodeValue + "\nB: " + itemlist[pid].firstChild.nodeValue);
if(itemlist[i].firstChild.nodeValue == itemlist[pid].firstChild.nodeValue)
alert("trovata: ");
}
}