473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

attach events to dynamically created elements

54 New Member
Hello,
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.               }
  13.  
  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
  25.  

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 1854
gits
5,390 Recognized Expert Moderator Expert
hi ...

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

kind regards
Mar 10 '08 #2

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

Similar topics

3
4084
by: Aymer | last post by:
i created a new button object. how do i attach an event on it? thanx for your help. Dim cc As ColorConverter = new ColorConverter() Dim b = new button() b.ID = "SelectedDate" b.BorderStyle = 1 b.forecolor = cc.ConvertFromString("red") b.BackColor = cc.ConvertFromString("white")
1
3193
by: Will | last post by:
Hi, I have a problem trying to validate dynamically created html form elements using javascript. I have dynamically created a check box using ASP for each record in a recordset and have given each a unique ID using the primary key from a db table. e.g "chk" + "1" for record 1 and "chk" + "2" for record 2 etc. This means each box is called chk1 and chk2 respectively. This works fine and changes dependant on the recordset used and allows me...
4
2067
by: Julie J. | last post by:
When working w/ forms and controls, in 95% of the cases, the value of the control is used somewhere in the program (duh!). However, there doesn't seem to be an easy way to attach a value variable to that control the automatically handles synchronization of the value w/ the contol. Here is an example: // some form
4
2561
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't know exactly where the table will be located on the page until runtime. Before the form control table is added to the Placeholder, I'm adding a whole bunch of tables to the Placeholder. This is a flowchart program and I have multiple action boxes...
1
1302
by: Bishop | last post by:
I have a page that generates a grid of controls; at the end of each row is an update button that is dynamically created with an associated click event. The grid changes based on the date selected in the calendar control and I populate this grid of controls by calling a routine in the page load event. My dilemma is that the page load event executes before my grid control event during a postback, thus the date that the grid references does...
1
3140
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
1
1852
by: Nirmalkumar | last post by:
How to attach an event to dynamically created control in VB.NET I have dynamically created dropdown list in code behind (VB.NET). For this control I want to attach an event for action ‘OnSelectedIndexChanged’. What is the property to assigne an event to a dynamically created dropdownlist
7
2344
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
2
1652
by: Nathan Sokalski | last post by:
I have LinkButtons that are dynamically created in one of the PostBack events. They must be created in the PostBack event because one of the variables required to determine which ones to create comes from the event arguments. Because the LinkButtons are not created in the Init event, they will not exist when I click them to perform a postback. How do I handle the events for these LinkButtons? I appreciate any help you can give. Thanks. --...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.