By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Problem with AttachEvent

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ivo

"PedroVasconcelos" <pm**@zmail.pt> wrote in message
news:e0**************************@posting.google.c om...
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
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>

Jul 20 '05 #2

P: n/a
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

P: n/a
"pmsv pmsv" <pm**@zmail.pt> wrote in message
news:40*********************@news.frii.net...
<snip>
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.
Jul 20 '05 #4

P: n/a
is that the best way? There isn't a way to ckeck the attachEvents
without having to save history??
Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5

P: n/a
pmsv pmsv <pm**@zmail.pt> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.