attach events to dynamically created elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sindhu
    New Member
    • Oct 2006
    • 53

    attach events to dynamically created elements

    Hello,
    I am able to attach event to dynamically created element.as shown below.

    [code=javascript]
    for(var i=0;i<10;i++)
    {for(var j=0;j<15;j++)
    {
    var div=document.cr eateElement('di v');
    div.setAttribut e("name","div"+ i+j);
    div.setAttribut e("id","div"+i+ j);
    var r=i;var c=j;
    div.attachEvent ("onclick",func tion(){ change_color('d iv'+i+j)});
    if(i==5 && j==7)
    {
    div.style.backg roundColor='blu e'; //Making the centre one blue;
    }

    div.style.left= 60+(j*15)+"px";//document.frm_ca m_copyset.btn_S et.type input.type='but ton';
    div.disabled=tr ue;
    //input.style.bac kgroundColor='t ransparent';
    div.style.posit ion="absolute";
    div.style.top=1 70+(i*15)+"px";
    div.style.width =13+"px";
    div.style.heigh t=13+"px";
    div.style.borde r="1px solid #ff0000";
    p_d.appendChild (div);
    }
    }//alert(document. g
    [/code]


    This code creates a table like look containing div s as cells of the table.this is to enable a perticular area to be selected using onclick event on div.
    The problem i'm facing is,
    I'm not able to attach the event 'onclick' to thse cells as shown in bold in the code.
    change_color() is the function that must be called everytime i click the div.i'm passing the id of the div.
    Whenever i pass the id of this div the id of the last element is getting fetched.Please help me
    Last edited by gits; Mar 10 '08, 12:42 PM. Reason: fix code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    have a look at this thread where an analogue problem is handled ...

    kind regards

    Comment

    Working...