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

Problem with AttachEvent

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
5 4702
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: knoak | last post by:
Hi there, I have a php-generated page which can have a variety of layouts, depending on a lot of things. But every layout has the same <head> and <body> so i cannot put an onload in the <body>...
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
5
by: Bert | last post by:
Hello, I'm having some problems with creating a script that works on both Mozilla browsers as IE. I want to change the background color of textareas and input text fields as soon as somebody...
18
by: luco | last post by:
Hi! I'm having a problem with attachEvent function. I'd like to add attachEvent dynamically to some objects so that each could execute event function with different parameter value. The question...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
4
by: Andrew Ip | last post by:
Hi everyone, I'm trying to dynamically create an image map for a particular image on my website, and I'm running into an issue where I try to register the "mouseover" and "mouseout" events for...
2
by: davidson1 | last post by:
Hai friends..for menu to use in my website..i found in one website....pl look below website.... http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm i downloaded 2 files.... ...
3
by: rahulgupta | last post by:
i have a textbox and a save_btn which is a hyperlink. when ever enter key is pressed there is a javascript which check if the name in the textbox contains any special characters. but when we press...
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?
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...
0
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...

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.