473,513 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why i can't use attachEvent with innerHTML;

if i use AttachEvent like this,it can't work;
eg:

var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";
but i use attachEvent like this,it work;
eg:

var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var text = document.createElement("span");
text.innerHTML="<-click this";
var div = documet.createElement("div");

div.appendChild(img); //can work
div.appendChild(text);

Apr 27 '07 #1
6 2297
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
if i use AttachEvent like this,it can't work;
eg:

var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";

but i use attachEvent like this,it work;
eg:

var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var text = document.createElement("span");
text.innerHTML="<-click this";
var div = documet.createElement("div");

div.appendChild(img); //can work
div.appendChild(text);
Was there a problem with the answer I supplied before?

<URL: http://groups.google.com/group/comp.lang.javascript/msg/de4303743575ead4>

Peter

Apr 27 '07 #2
On 4月27日, 上午10时29分, Peter Michaux <petermich....@gmail.comwrote:
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
if i use AttachEvent like this,it can't work;
eg:
var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";
but i use attachEvent like this,it work;
eg:
var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var text = document.createElement("span");
text.innerHTML="<-click this";
var div = documet.createElement("div");
div.appendChild(img); //can work
div.appendChild(text);

Was there a problem with the answer I supplied before?

<URL:http://groups.google.com/group/comp.lang.javascript/msg/de4303743575ead4>

Peter
thx;
but it also can't work like this;
img.attachEvent("onclick",function(){alert("test") });

when i use the attachEvent and use innerHTML . the event can't work;

Apr 27 '07 #3
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
if i use AttachEvent like this,it can't work;
eg:

var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";
The above line removes the image from the div. InnerHTML removes all
other children from the element.

replace this
div.innerHTML="<-click this";
with

div.appendChild(document.createTextNode('<--click this'));

You should be able to see both the image and the text this way.

Peter

Apr 27 '07 #4
On 4月27日, 上午11时15分, Peter Michaux <petermich....@gmail.comwrote:
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
if i use AttachEvent like this,it can't work;
eg:
var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";

The above line removes the image from the div. InnerHTML removes all
other children from the element.

replace this
div.innerHTML="<-click this";

with

div.appendChild(document.createTextNode('<--click this'));

You should be able to see both the image and the text this way.

Peter
can i use this
div.innerHTML+="<--click this" ?

Apr 27 '07 #5
On Apr 26, 8:18 pm, SkyZhao <sky.zhao....@gmail.comwrote:
On 4鏈27鏃, 涓婂崍11鏃15鍒, PeterMichaux <petermich...@gmail.comwrote:
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
if i use AttachEvent like this,it can't work;
eg:
var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";
The above line removes the image from the div. InnerHTML removes all
other children from the element.
replace this
div.innerHTML="<-click this";
with
div.appendChild(document.createTextNode('<--click this'));
You should be able to see both the image and the text this way.
Peter

can i use this
div.innerHTML+="<--click this" ?
You can but I probably wouldn't since this requires conversion of dom
to string and back to dom for the image. That is unnecessary.

Peter

Apr 27 '07 #6
SkyZhao said the following on 4/26/2007 11:18 PM:
On 4鏈27鏃, 涓婂崍11鏃15鍒, Peter Michaux <petermich...@gmail.comwrote:
>On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@gmail.comwrote:
>>if i use AttachEvent like this,it can't work;
eg:
var img = document.createElement("img");
img.attachEvent("onclick",alert("test"));
var div = document.createElement("div");
div.appendChild(img); //can't work;
div.innerHTML="<-click this";
The above line removes the image from the div. InnerHTML removes all
other children from the element.

replace this
>>div.innerHTML="<-click this";
with

div.appendChild(document.createTextNode('<--click this'));

You should be able to see both the image and the text this way.

Peter

can i use this
div.innerHTML+="<--click this" ?
Did you try it? Or are you just guessing wanting someone to do it for you?

--
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 27 '07 #7

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

Similar topics

4
5413
by: John McCarthy | last post by:
Novice here - using notepad only ..... function MenuBarIn(E) { var i; with (document.getElementById(E.id)) {
5
4706
by: PedroVasconcelos | last post by:
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"); }...
6
4057
by: Adrian Parker | last post by:
Using asp.net 2003 When I use document.attachEvent("onmousemove", resetTimer); The first time into a page, it works fine, but the 2nd time the page is loaded (not postback) it gives a...
5
14445
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
12036
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
12548
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',...
3
19553
by: SkyZhao | last post by:
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...
5
2201
by: dwmartin18 | last post by:
Hello everyone. I have quite the puzzling problem with a script I have been working on lately. I have created a function that can be called to create a new html element (e.g. input, select, div,...
0
7267
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
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
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
7553
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
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing,...
1
5100
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...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.