Problem with AttachEvent

P

PedroVasconcelos

Hello everyone!
I am trying to copy AttachEvents but i don't know how.
Because innerHTML doesn't copy the AttachEvents.
Here is an example:

<script>
function a(){
alert("is changing");
}
</script>

<div id="d1" name="d1">
<select id="s1" name="s1">
<option>A</option>
<option>B</option>
</select>
</div>

<script>
s1.attachEvent("onchange",a);
</script>

<div id="d2" name="d2">
</div>

<script>
d2.innerHTML=d1.innerHTML;
</script>

Can anyone help me?
 
I

Ivo

PedroVasconcelos said:
Hello everyone!
I am trying to copy AttachEvents but i don't know how.
Because innerHTML doesn't copy the AttachEvents.

No, because you end up with two elements with id="s1" and that won't work of
course as id's need to be unique. I changed your last line into:

if(s1) alert(s1.tagName); // SELECT
d2.innerHTML=d1.innerHTML;
if(s1) alert(s1.tagName); // undefined

HTH
Ivo
 
P

pmsv pmsv

Thanks for your reply but that is not the problem as you can see with
this example:

script>
function merda(){
alert("mudou");
}
</script>

<div id="d1" name="d1">
<select id="s1" name="s1">
<option>A</option>
<option>B</option>
</select>
</div>

<div id="d2" name="d2">
</div>

<script>
s1.attachEvent("onchange",merda);
</script>

<script>
var i1=d1.innerHTML;
d1.innerHTML="";
d2.innerHTML=i1;
alert(s1.tagName);

</script>

the innerHTML does not pass the attachEvent. The innerHTML is only a
string and if you look at it, you don't have any reference to the
attachEvent.
So the question remains: How can i copy AttachEvents?
 
R

Richard Cornford

So the question remains: How can i copy AttachEvents?

By keeping track of what you attach to which elements and then when you
reproduce one of those elements by any means you repeat the same event
handler attaching operations as have been applied to the original to the
new element.

Richard.
 
P

pmsv pmsv

is that the best way? There isn't a way to ckeck the attachEvents
without having to save history??
Thanks
 
L

Lasse Reichstein Nielsen

pmsv pmsv said:
is that the best way? There isn't a way to ckeck the attachEvents
without having to save history??

No. Unlike the simple "onclick" property, there is no way to get at
the events added with attachEvent (or the non-proprietary
addEventListener). You must even know them in order to remove them
with detachEvent (or removeEventListener).

I wouldn't use innerHTML to copy DOM nodes at all, though. I would
rather use the DOM method cloneNode with a true argument to make
it a deep clone.

In your case, you don't even make a copy, you just move the nodes.
You can do that directly:

while(d1.hasChildNodes()) {
d2.appendChild(d1.firstChild);
}

This has the advantage of moving the DOM nodes themselves, including
their attached event listeners.

It seems you are writing for IE only (using attachEvent). Otherwise,
you should remember to initialize the variables d1 and d2.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top