Forum Moderators: open

Message Too Old, No Replies

dynamic attribution of onclicks...

         

SpaceFrog

3:32 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



this code is included in a loop wher i varies from 0 to 26

switch (Type_ligne) {
// TITRE DE MENU
case "titre" :

var ligneTitre=document.getElementById('som').getElementsByTagName('tr')[0].cloneNode(true);
ligneTitre.lien=i;
ligneTitre.setAttribute('onclick',function(){alert(ligneTitre.lien)});
ligneTitre.onclick=function(){alert(ligneTitre.lien)};
var TitreCells=ligneTitre.getElementsByTagName('td')
TitreCells[1].style.backgroundColor=TabTextBase[i].split(String.fromCharCode(9))[ColPhot];
TitreCells[1].style.cursor="pointer";
TitreCells[1].firstChild.data=TabTextBase[i].split(String.fromCharCode(9))[Nom];
document.getElementById('som').appendChild(ligneTitre);
var TitreCatal=document.getElementById('catal').getElementsByTagName('tr')[0].cloneNode(true);
TitreCatal.style.display='';
TitreCatal.id=ligneTitre.lien;
var CellCatal=TitreCatal.getElementsByTagName('td');
CellCatal[1].style.backgroundColor=TabTextBase[i].split(String.fromCharCode(9))[ColPhot];
CellCatal[1].firstChild.data=TabTextBase[i].split(String.fromCharCode(9))[Nom];
document.getElementById('catal').appendChild(TitreCatal);

newMenu=true
menuNode=1;
menuLines=0;
break;

case "menu":
ligneMenu=(newMenu)?document.getElementById('som').getElementsByTagName('tr')[1].cloneNode(true):document.getElementById('som').getElementsByTagName('tr')[document.getElementById('som').getElementsByTagName('tr').length-1];
ligneMenu.style.display='';
var MenuCells=ligneMenu.getElementsByTagName('td')
var MenuDiv=document.createElement('div');
MenuDiv.className="menuDiv";
MenuDiv.style.cursor='pointer';
var MenuText=TabTextBase[i].split(String.fromCharCode(9))[Nom]+String.fromCharCode(10)
MenuDiv.lien=i;
MenuDiv.setAttribute('onclick',function(){alert(MenuDiv.lien)});
MenuDiv.onclick=function(){alert(MenuDiv.lien)};
MenuDiv.appendChild(document.createTextNode( MenuText)) ;
MenuCells[menuNode].appendChild(MenuDiv)

menuLines++;
if(newMenu){
document.getElementById('som').appendChild(ligneMenu);
newMenu=false;
}

if(menuLines==3){
menuNode++;
menuLines=0;
}


break;

in the first case I get an alert that varies from 0 to 26 according to element clicked ...
in the second case I unvariably get 26?

I really do not understand where I am goofing on this one?

SpaceFrog

8:37 am on Jul 28, 2005 (gmt 0)

10+ Year Member



ok Got it!

ligneTitre.setAttribute('onclick',function(){alert(this.lien)});
ligneTitre.onclick=function(){alert(this.lien)};

otherwise you always end up with the last occurence of i ...