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

how to use "attachEvent"?;

i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
div.innerHTML+="some text";

the event can not work; why can't i use it?

if i use nether code , it can work;

code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
spanWord = document.createElement("span");
spanWord.innerHTML="some text";
div.appendChild(spanWord);

do anybody know the reason?

Apr 25 '07 #1
3 19544
SkyZhao said the following on 4/24/2007 10:00 PM:
i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
div.innerHTML+="some text";
img.onclick= function(){alert('aa')}

never did care for attachEvent.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 25 '07 #2
On Apr 24, 7:00 pm, SkyZhao <sky.zhao....@gmail.comwrote:
i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));
Above you have used a function call as the second argument attachEvent

The second argument to attachEvent is a function...

img.attachEvent("onclick",function(){alert{"aa");} );
div.appendChild(img);
div.innerHTML+="some text";
Peter

Apr 26 '07 #3
On Apr 24, 7:36 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
SkyZhao said the following on 4/24/2007 10:00 PM:
i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
div.innerHTML+="some text";

img.onclick= function(){alert('aa')}

never did care for attachEvent.
Loose coupling between chunks of JavaScript and event delegation must
not be your thing, eh? It is relatively frequently that I attach more
than one window.onload handler to initialize javascript pieces that
deal with different parts of the page.

Peter

Apr 26 '07 #4

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

Similar topics

6
by: Yvan J. Gagnon | last post by:
I am currenly developing a web site using Macromedia fireworks, and am trying to figure out a way (through hand-coding) of attaching a javascript function (onClick="doit=false") to each of the...
1
by: Robert Oschler | last post by:
I've successfully attached to the "onclick" and several other events using AttachEvent(). But when I try to attach to the "onbeforeunload" event it doesn't work (the handler I've assigned is never...
2
by: metasilk | last post by:
Situation: Many links with hrefs & some have ids in the HTML. External javascript file. Wanted: When the mouse is hovering over a link with a certain id, do something (such as display one...
5
by: tuxedo | last post by:
The way the <body onload="something()"works ensures that the complete html document is loaded before something() is executed. Can the same be achieved when placing the onload call in document...
6
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
i'd like to intercept the window.onload event in order to distribute it, as needed, to several methods. example : suppose i have several methods doing unlinked initialisations: ...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
1
RMWChaos
by: RMWChaos | last post by:
I grabbed this "Rock Solid addEvent" code from this site, which is based on Mark Wubben's event-cache code. (These links for reference only.) I am having two problems with it, and the webmaster is...
6
by: K Viltersten | last post by:
It's perhaps something elementary. I tried to do suff as follows. document.getElementById ('Button1').on ( 'click', funcion () {}); For some reason, the method isn't there. According to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.