470,594 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

attach events to dynamically created elements

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

Expand|Select|Wrap|Line Numbers
  1. for(var i=0;i<10;i++)
  2.      {for(var j=0;j<15;j++)
  3.          {     
  4.                var div=document.createElement('div');
  5.                div.setAttribute("name","div"+i+j);
  6.                div.setAttribute("id","div"+i+j); 
  7.                var r=i;var c=j;
  8.                div.attachEvent("onclick",function(){ change_color('div'+i+j)});
  9.               if(i==5 && j==7)
  10.               {
  11.                   div.style.backgroundColor='blue'; //Making the centre one blue;
  12.               }
  14.               div.style.left=60+(j*15)+"px";//document.frm_cam_copyset.btn_Set.type         input.type='button';
  15.               div.disabled=true;
  16.               //input.style.backgroundColor='transparent';
  17.               div.style.position="absolute";
  18.               div.style.top=170+(i*15)+"px";
  19.               div.style.width=13+"px";
  20.               div.style.height=13+"px";
  21.               div.style.border="1px  solid #ff0000";              
  22.               p_d.appendChild(div);                        
  23.          }
  24.      }//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
Mar 10 '08 #1
1 1748
5,390 Expert Mod 4TB
hi ...

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

kind regards
Mar 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Aymer | last post: by
1 post views Thread by Will | last post: by
4 posts views Thread by Julie J. | last post: by
4 posts views Thread by blue | last post: by
1 post views Thread by Bishop | last post: by
2 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.