473,387 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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="javascript">
function AddOneCtl() {
var ni = document.getElementById('myDiv');
var newctl2 = document.createElement('button');
newctl2.setAttribute('id','Test1');
newctl2.setAttribute('value','Click Me');
ni.appendChild(newctl2)
document.getElementById('Test1').attachEvent('onCl ick',clickevt1);
}

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

</script>

</HEAD>

<BODY>
<FORM name="AddControl">
<INPUT TYPE="button" VALUE="Click here to add a control"
onClick="AddOneCtl()">
</FORM>

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

</BODY>
</HTML>

Apr 11 '06 #1
2 5165


jiayanxiang wrote:

document.getElementById('Test1').attachEvent('onCl ick',clickevt1);


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 addEventListener.

Simply setting
elementNode.onclick = 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.getElementById('Test1').attachEvent('onCl ick',clickevt1);


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 addEventListener.

Simply setting
elementNode.onclick = 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
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...
2
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...
1
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...
2
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...
1
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...
0
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...
1
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...
4
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...
5
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.