473,699 Members | 2,383 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 5201


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
1396
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
2908
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
1453
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
1586
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
1746
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
1076
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
1796
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
2625
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
1958
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
4450
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
8622
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
9184
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
9045
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
8930
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,...
0
8892
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
7767
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4382
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
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
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.