I am able to attach event to dynamically created element.as shown below.
Expand|Select|Wrap|Line Numbers
- for(var i=0;i<10;i++)
- {for(var j=0;j<15;j++)
- {
- var div=document.createElement('div');
- div.setAttribute("name","div"+i+j);
- div.setAttribute("id","div"+i+j);
- var r=i;var c=j;
- div.attachEvent("onclick",function(){ change_color('div'+i+j)});
- if(i==5 && j==7)
- {
- div.style.backgroundColor='blue'; //Making the centre one blue;
- }
- div.style.left=60+(j*15)+"px";//document.frm_cam_copyset.btn_Set.type input.type='button';
- div.disabled=true;
- //input.style.backgroundColor='transparent';
- div.style.position="absolute";
- div.style.top=170+(i*15)+"px";
- div.style.width=13+"px";
- div.style.height=13+"px";
- div.style.border="1px solid #ff0000";
- p_d.appendChild(div);
- }
- }//alert(document.g
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