473,405 Members | 2,338 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,405 software developers and data experts.

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
..addEventListener It's particularly vexing because if you do
element.cloneNode(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 addEventListener, and removeEventListener 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:red">This is some
starter text in a textarea</textarea>
<button type=button onclick="replaceWithClone(elem('foo'))"
accesskey=c><u>C</u>lone</button>
</form>

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

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

var oldREL = elem.addEventListener;
elem.removeEventListener =
function (oldREL, aListeners) {
return function (evt, func, bCapture) {
for (var i=aListeners.length;--i;)
if (aListeners[i][0]==evt && aListeners[i][1]==func &&
aListeners[i][2]==bCapture)
aListeners.splice(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.addEventListener = this.addEventListener;
elem.removeEventListener = this.removeEventListener;
elem.cloneNode = this.cloneNode;
if (elem.nodeName=="TEXTAREA") elem.value = this.value;
for (var i=0;i<aListeners.length;++i) {
var aList=aListeners[i];
this.removeEventListener(aList[0], aList[1], aList[2]);
oldAEL.call (elem, aList[0], aList[1], aList[2]); }
return elem; } } (oldAEL, aListeners, oldClone);
}

function replaceWithClone(ta) {
var newTa = ta.cloneNode(true);
var start = ta.selectionStart, end=ta.selectionEnd;
ta.parentNode.replaceChild(newTa, ta);
// Doesn't work properly with reverse selections
newTa.setSelectionRange (start, end);
}
</script>

Apr 24 '06 #1
2 4575
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
.addEventListener It's particularly vexing because if you do
element.cloneNode(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 addEventListener, and removeEventListener 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
.addEventListener It's particularly vexing because if you do
element.cloneNode(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 addEventListener, and removeEventListener 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
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: ...
1
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 =...
2
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...
17
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...
0
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...
1
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...
0
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
0
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...
0
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...

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.