473,597 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to AttachEvent for dynamically added controls?

Hi:

I used the following code to dynamically add a control and I tried to
use attachevent to add a onClick event to the control. It doesn't seem
to do anything. Any idea what might be wrong?

Thanks.

<HTML>
<HEAD>

<script language="javas cript">
function AddOneCtl() {
var ni = document.getEle mentById('myDiv ');
var newctl2 = document.create Element('button ');
newctl2.setAttr ibute('id','Tes t1');
newctl2.setAttr ibute('value',' Click Me');
ni.appendChild( newctl2)
document.getEle mentById('Test1 ').attachEvent( 'onClick',click evt1);
}

function clickevt1() {
alert("Clicked" );
}

</script>

</HEAD>

<BODY>
<FORM name="AddContro l">
<INPUT TYPE="button" VALUE="Click here to add a control"
onClick="AddOne Ctl()">
</FORM>

<div id="myDiv"> </div>

</BODY>
</HTML>

Apr 11 '06 #1
2 5186


jiayanxiang wrote:

document.getEle mentById('Test1 ').attachEvent( 'onClick',click evt1);


It should be 'onclick', not 'onClick'. However note that attachEvent is
part of the IE 5 DOM, some other browsers like Opera implement that too,
but Mozilla needs the W3C DOM specific addEventListene r.

Simply setting
elementNode.onc lick = clickevt1;
should suffice however.

Additionally the HTML button element is not a good choice on the web, it
has different default values for the type attribute (e.g. 'submit' for
Mozilla, 'button' for IE) and IE insists on submitting the content
(innerText) of the button element if it is a submit button.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 11 '06 #2
Thanks, it worked fine now.

The page I am working on is internal and for IE only. (We can't even
run firefox here:( ) so it simplifies things a little.
Martin Honnen wrote:
jiayanxiang wrote:

document.getEle mentById('Test1 ').attachEvent( 'onClick',click evt1);


It should be 'onclick', not 'onClick'. However note that attachEvent is
part of the IE 5 DOM, some other browsers like Opera implement that too,
but Mozilla needs the W3C DOM specific addEventListene r.

Simply setting
elementNode.onc lick = clickevt1;
should suffice however.

Additionally the HTML button element is not a good choice on the web, it
has different default values for the type attribute (e.g. 'submit' for
Mozilla, 'button' for IE) and IE insists on submitting the content
(innerText) of the button element if it is a submit button.


--

Martin Honnen
http://JavaScript.FAQTs.com/


Apr 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1391
by: Paul | last post by:
An early Happy Thanksgiving to you and yours... We develop data driven apps. We dynamically place controls on a panel which may be then be placed on a web form or another control. My problem is that I can't get the events of the hosted controls to fire. e.g. ' myUserControl.ascx. is placed on myWebForm.aspx in the Designer ' myUserControl.ascx has a Panel on it, Panel1 ' From code in myUserControl.ascx I pass Panel1 ByRef to a class...
2
2902
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to avoid the problem by avoiding the table control or resorting to databound controls that better manage state for me. I hope to understand how to solve the problem by using the Table web control and sticking to the approach of building the table at run...
1
1448
by: Christian Nunciato | last post by:
Hi there: I've checked the forums, but haven't found a situation addressing my specific problem. Here's the deal: I've got a Web Form containing two Panel controls. One displays search results in a DataList comprised mainly of LinkButtons. The other's basically empty; it's intended to handle a collection of dynamically added controls. When the user clicks a LinkButton from within the first Panel control, I
2
1585
by: wapsiii | last post by:
I have a form to which I add sets of input controls ie. textbox 1, dropdownlist 1, textbox 2, dropdownlist 1, textbox 3, dropdownlist 3. The number of sets vary. I give them unique IDs the following way: txt_1, ddl_1, txt_2, ddl_2, txt_3, ddl_3. When the form is submitted I retrieve the values of the dynamically added controls by iterating over the request.form collection looking for IDs txt_ and ddl_
1
1742
by: keithb | last post by:
I have found that I must re-create dynamically added controls on every postback in order to find and access them programatically. The controls I am working with are inside a GridView control. When the edit button is clicked, a postback occurs and the control are re-created. This causes an additional set of columns to be created in the GridView control, so that all of the dynamically added controls exist in 2 places on the screen. How can I...
0
1068
by: keithb | last post by:
I posted this question earlier with an unclear predicate. Let me give it another shot: I have found that unless I re-create dynamically added controls on every postback, I am unable to access them programatically. When I do re-create them on postback, I can access them programmatically; however, the controls appear on my webpage in uplate. How can I recreate the controls on postback without having them appear in duplicate? Thanks,
1
1785
by: Mike | last post by:
Hi, I am having troubles dealing with dynamically created WebControls. I have this HTML code: <form id="Form1" runat="server" method="post"> <table width="960" align="center"> <tr> <td align="center"> ......
4
2624
by: Chris | last post by:
I have some code which adds controls to a placeholder. The button and label however always appears above the dynamically added content when the actual tags are below. Why is this. I want them to appear below as it really messes with my plans for assessiblity. What am I doing wrong. Regards. Dim Form1 As New HtmlForm Form1 = Page.FindControl("form1") Dim masterplaceholder As PlaceHolder = New PlaceHolder masterplaceholder =...
5
1954
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before some textboxes (dynamically added). I know I could move it around with CSS but I want to move towards an accessible site that will display forms in the right order without CSS. Regards, Chris.
5
4447
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve the values of the controls when you perform a postback, why add the controls in the firstplace?...
0
7962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
7884
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
8267
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
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3880
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...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
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
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1229
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.