473,772 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cloneNode and iterating event listeners

One of the things that I never understood was the motivation for not
being able to iterate over the event listeners added via
..addEventListe ner It's particularly vexing because if you do
element.cloneNo de(bDeep), you frequently get a stunted clone, often
with several key features missing, and one of these is associated event
handlers. Of course, it could be the case that the event handlers have
a closure including a reference to the original node, but still...

So I rewrote addEventListene r, and removeEventList ener so that
cloneNode could be accomodated. The below is a framework, and needs
some work, (including the transfer of attributes).

Csaba Gabor from Vienna

How it works. When you enter a character, you get a message box.
This is what lets you verify that the cloning of event handlers
worked. Before cloning, make a selection to see that the selection
is transferred, too.

<form action="" method=get>
<textarea id=foo name=foo style="color:re d">This is some
starter text in a textarea</textarea>
<button type=button onclick="replac eWithClone(elem ('foo'))"
accesskey=c><u> C</u>lone</button>
</form>

<script type='text/javascript'>
function elem(id) { return document.getEle mentById(id); }
fixCloneNode(el em('foo'));
elem('foo').add EventListener(" input",
function() { alert("New text: " + this.value); }, false);

function fixCloneNode(el em) {
var oldAEL = elem.addEventLi stener;
var aListeners = [];
elem.addEventLi stener =
function (oldAEL, aListeners) {
return function (evt, func, bCapture) {
oldAEL.call (this, evt, func, bCapture)
aListeners.push ( [evt, func, bCapture] ); } }
(oldAEL, aListeners);

var oldREL = elem.addEventLi stener;
elem.removeEven tListener =
function (oldREL, aListeners) {
return function (evt, func, bCapture) {
for (var i=aListeners.le ngth;--i;)
if (aListeners[i][0]==evt && aListeners[i][1]==func &&
aListeners[i][2]==bCapture)
aListeners.spli ce(i,1);
} } (oldREL, aListeners);

var oldClone = elem.cloneNode;
elem.cloneNode =
function (oldAEL, aListeners, oldClone) {
return function (bDeep) {
// does not reproduce the event listeners
var elem = oldClone.call (this, bDeep);
elem.addEventLi stener = this.addEventLi stener;
elem.removeEven tListener = this.removeEven tListener;
elem.cloneNode = this.cloneNode;
if (elem.nodeName= ="TEXTAREA") elem.value = this.value;
for (var i=0;i<aListener s.length;++i) {
var aList=aListener s[i];
this.removeEven tListener(aList[0], aList[1], aList[2]);
oldAEL.call (elem, aList[0], aList[1], aList[2]); }
return elem; } } (oldAEL, aListeners, oldClone);
}

function replaceWithClon e(ta) {
var newTa = ta.cloneNode(tr ue);
var start = ta.selectionSta rt, end=ta.selectio nEnd;
ta.parentNode.r eplaceChild(new Ta, ta);
// Doesn't work properly with reverse selections
newTa.setSelect ionRange (start, end);
}
</script>

Apr 24 '06 #1
2 4601
Csaba Gabor wrote:
One of the things that I never understood was the motivation for not
being able to iterate over the event listeners added via
.addEventListen er It's particularly vexing because if you do
element.cloneNo de(bDeep), you frequently get a stunted clone, often
with several key features missing, and one of these is associated event
handlers. Of course, it could be the case that the event handlers have
a closure including a reference to the original node, but still...

So I rewrote addEventListene r, and removeEventList ener so that
cloneNode could be accomodated. The below is a framework, and needs
some work, (including the transfer of attributes).
[...]
How it works. [...]


For appropriate values of "works". It has been pointed out many times
that it is error-prone to assume that you can add or modify properties
of host objects arbitrarily. You do it anyway.
PointedEars
--
When you have eliminated all which is impossible, then
whatever remains, however improbable, must be the truth.
-- Sherlock Holmes
Apr 24 '06 #2
Another PointedEars INC post.

Thomas 'PointedEars' Lahn wrote:
Csaba Gabor wrote:
One of the things that I never understood was the motivation for not
being able to iterate over the event listeners added via
.addEventListen er It's particularly vexing because if you do
element.cloneNo de(bDeep), you frequently get a stunted clone, often
with several key features missing, and one of these is associated event
handlers. Of course, it could be the case that the event handlers have
a closure including a reference to the original node, but still...

So I rewrote addEventListene r, and removeEventList ener so that
cloneNode could be accomodated. The below is a framework, and needs
some work, (including the transfer of attributes).
[...]
How it works. [...]
For appropriate values of "works".


Whatever
It has been pointed out many times
And you don't even back it up with a single reference? For shame.
that it is error-prone to assume
Assuming things is error prone in general.
that you can add or modify properties of host objects arbitrarily.
And this affects me how? Rather than making unsubstantiated and/or
irrelevant claims, it would be preferable to have either a concrete
counterexample or offer a method to improve upon, or at least
complement the subject matter of the post.
You do it anyway.


You do neither.

Apr 25 '06 #3

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

Similar topics

6
2339
by: Amir Hardon | last post by:
I am dynamically adding rows to a table, and each row have a button which removes it. I have successfully implemented this for mozilla but I'm having troubles with IE, here is how I did it: Each new row's id is an index number, each button's name is the row's id, and I'm adding this function as the click event listener for the button: function removeline(event){ var row=document.getElementById("row" + event.target.name);
1
2125
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML = tmpHTML Before the changes, client-side .Net field validators (RegularExpressionValidator, RangeValidator, etc.) work fine... after the changes, the modified HTML works correctly but the validators stop functioning. Seems that the onchange handler...
2
11366
by: kj | last post by:
How does one trigger an event programmatically? I'm interested in how to do this in both the "Level 0" event model as well as in the DOM Level 2 event model. Thanks! kj -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded.
17
4883
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
0
2075
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even if it doesn't exist yet. The event manager uses weakrefs, so lists of listeners won't stop them
1
3464
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my control. The event handler is displayed on VB6 IDE combo and you can create a sub for it as usual, but when I run the vb6 form to test it, it won't work. I have spent too much time til now with that without finding solution. Can anybody help me ?
0
5011
by: Eniac | last post by:
Hello, I've started using Enterprise Library 2.0 recently and I've encountered a problem that seems to be ... well... undocumented :) Basically, when I set a Trace Listener (formatted event log to be precise), if i specify the name of a custom event log, the listener won't log in it. I've checked in the registry, the log is there. In the event viewer, I
2
1988
by: meyousikmann | last post by:
This will be difficult to explain so bear with me. If anyone is familiar with Tibco Rendezvous and/or Microsoft Messaging, this may make more sense. I've created a hierarchy of objects that looks something like this: Tibco-->Transports-->Transport Where Tibco is the overall component that owns a collection of Transports and each Transport is an object of a class.
0
9619
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
10261
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
10103
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
10038
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
8934
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...
1
7460
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3
2850
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.