473,614 Members | 2,352 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.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var div = document.create Element("div");
div.appendChild (img); //can't work;
div.innerHTML=" <-click this";
but i use attachEvent like this,it work;
eg:

var img = document.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var text = document.create Element("span") ;
text.innerHTML= "<-click this";
var div = documet.createE lement("div");

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

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

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

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

var img = document.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var text = document.create Element("span") ;
text.innerHTML= "<-click this";
var div = documet.createE lement("div");

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

<URL: http://groups.google.c om/group/comp.lang.javas cript/msg/de4303743575ead 4>

Peter

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

Was there a problem with the answer I supplied before?

<URL:http://groups.google.c om/group/comp.lang.javas cript/msg/de4303743575ead 4>

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

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....@g mail.comwrote:
if i use AttachEvent like this,it can't work;
eg:

var img = document.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var div = document.create Element("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.creat eTextNode('<--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....@g mail.comwrote:
if i use AttachEvent like this,it can't work;
eg:
var img = document.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var div = document.create Element("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.creat eTextNode('<--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....@g mail.comwrote:
On 4鏈27鏃, 涓婂崍11鏃15鍒 , PeterMichaux <petermich...@g mail.comwrote:
On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@g mail.comwrote:
if i use AttachEvent like this,it can't work;
eg:
var img = document.create Element("img");
img.attachEvent ("onclick",aler t("test"));
var div = document.create Element("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.creat eTextNode('<--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...@g mail.comwrote:
>On Apr 26, 7:13 pm, SkyZhao <sky.zhao....@g mail.comwrote:
>>if i use AttachEvent like this,it can't work;
eg:
var img = document.create Element("img");
img.attachEve nt("onclick",al ert("test"));
var div = document.create Element("div");
div.appendChi ld(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.appendChil d(document.crea teTextNode('<--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.javas cript 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
5417
by: John McCarthy | last post by:
Novice here - using notepad only ..... function MenuBarIn(E) { var i; with (document.getElementById(E.id)) {
5
4717
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"); } </script>
6
4066
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 permission denied error.. any ideas why ? Thanks
5
14455
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 has entered something in them. The script below works perfectly in mozilla browsers (netscape 7, firefox 1,...) but doesn't in internet explorer (but it gives no
18
12054
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 is: how to pass the parameter to event function? The following code obviously doesn't work, how should i modify it to get it to work?
5
12571
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', makeEventFunc1(strand)); inp.attachEvent('onchange', makeEventFunc2(strand)); individually work in IE, but when used together, only the bottom one remains active. I have also tried
3
19562
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 nether code , it can work;
5
2217
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, etc.). It is used as follows: addElementToPage("writeroot", "input", "type:button, text:testText, value:testvalue, onclick:test1") The first argument is an ID of the location where the new element it to be appended. The second argument is the type...
0
8198
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
8142
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8444
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6093
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.