473,395 Members | 1,653 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,395 software developers and data experts.

innerHTML and event handlers

Hi all,

I have the following problem. In my page i have a large <div> with tags
inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the page
has fully loaded, all of those event handlers are dropped: the actions for
the event are not executed anymore.

I can't find anything related to this problem on the internet. Has anyone
ever encountered a similar problem?

TIA,

regs,

Tim
Jul 23 '05 #1
6 2799
That suggests to me that there is a javascript error somewhere that you're
encountering just after the line you show. An error can cause all scripts
on a page to stop functioning - or more precisely, cause the browser to stop
running them. What's going on around that line?

- Wm
--
William Morris
Product Development, Seritas LLC
Kansas City, Missouri

"Tim Fooy" <th*****@invalid.com> wrote in message
news:40***********************@news.skynet.be...
Hi all,

I have the following problem. In my page i have a large <div> with tags
inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the page
has fully loaded, all of those event handlers are dropped: the actions for
the event are not executed anymore.

I can't find anything related to this problem on the internet. Has anyone
ever encountered a similar problem?

TIA,

regs,

Tim

Jul 23 '05 #2

"Tim Fooy" <th*****@invalid.com> wrote in message
news:40***********************@news.skynet.be...
Hi all,

I have the following problem. In my page i have a large <div> with tags
inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the page
has fully loaded, all of those event handlers are dropped: the actions for
the event are not executed anymore.

I can't find anything related to this problem on the internet. Has anyone
ever encountered a similar problem?

TIA,

regs,

Tim


It's not really a problem, rather it is just the way it works. I suggest
repacing the innerHTML calls with using the DOM
(http://www.quirksmode.org/?dom/contents.html for reference) or just add and
empty SPAN tag or something as the first element in the DIV and perform the
innerHTML on the SPAN element.


Jul 23 '05 #3
> > Hi all,

I have the following problem. In my page i have a large <div> with tags
inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the page has fully loaded, all of those event handlers are dropped: the actions for the event are not executed anymore.

I can't find anything related to this problem on the internet. Has anyone ever encountered a similar problem?
It's not really a problem, rather it is just the way it works. I suggest
repacing the innerHTML calls with using the DOM
(http://www.quirksmode.org/?dom/contents.html for reference) or just add

and empty SPAN tag or something as the first element in the DIV and perform the innerHTML on the SPAN element.


I don't understand that "that's the way it works". Inside another <div> on
my page the same situation happens, and the event handlers inside that <div>
keep working after the innerHTML line.
Anyway, thanks for the suggestions. I'll try them out tomorrow.

Tim
Jul 23 '05 #4
"William Morris" schreef:
That suggests to me that there is a javascript error somewhere that you're
encountering just after the line you show. An error can cause all scripts
on a page to stop functioning - or more precisely, cause the browser to stop running them. What's going on around that line?


If i comment out the mentioned line, everything keeps working. There is
also no script error indication in the statusbar of IE.

Tim
Jul 23 '05 #5
"Tim Fooy" wrote:
Hi all,

I have the following problem. In my page i have a large <div> with tags inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the page has fully loaded, all of those event handlers are dropped: the actions for the event are not executed anymore.

I can't find anything related to this problem on the internet. Has anyone ever encountered a similar problem?
It's not really a problem, rather it is just the way it works. I suggest
repacing the innerHTML calls with using the DOM
(http://www.quirksmode.org/?dom/contents.html for reference) or just add

and
empty SPAN tag or something as the first element in the DIV and perform

the
innerHTML on the SPAN element.


I don't understand that "that's the way it works". Inside another <div>

on my page the same situation happens, and the event handlers inside that <div> keep working after the innerHTML line.
Anyway, thanks for the suggestions. I'll try them out tomorrow.


Both of your solutions solved the problem ;-) Thanks a lot!

Tim
Jul 23 '05 #6
On Wed, 28 Apr 2004 10:21:14 +0200, Tim Fooy <th*****@invalid.com> wrote:
I have the following problem. In my page i have a large <div> with tags
inside it that have event handlers on them (onclick etc.). When i run
div.innerHTML = moreText + div.innerHTML, either before or after the
page has fully loaded, all of those event handlers are dropped: the
actions for the event are not executed anymore.

I can't find anything related to this problem on the internet. Has
anyone ever encountered a similar problem?


Yes. I once tried to wrap a DIV inside another, dynamically created DIV.
All of the event handlers were removed:

<body>
<div id="outerDiv">
<div id="testDiv">
Page content
</div>
</div>
<script type="text/javascript">
var oD = document.getElementById( 'outerDiv' );
var tD = document.getElementById( 'testDiv' );

tD.onclick = function() {
alert( 'original' );
};

oD.innerHTML = '<div>' + oD.innerHTML + '</div>';
</script>
</body>

This even fails if attachEvent() or addEventListener() is used to add the
listener.

The solution for recent browser versions is to use DOM methods to create
and insert new nodes:

<body>
<div id="outerDiv">
<div id="testDiv">
Page content
</div>
</div>
<script type="text/javascript">
var nD = document.createElement( 'DIV' );
var oD = document.getElementById( 'outerDiv' );
var tD = document.getElementById( 'testDiv' );

tD.onclick = function() {
alert( 'original' );
};

nD.appendChild( oD.replaceChild( nD, tD ));
</script>
</body>

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #7

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

Similar topics

4
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
5
by: Amit | last post by:
Hello! Is it possible to find how many event handlers an event has at runtime? How about finding whether or not an event has event handlers? In C# you can compare the event with null to check if...
7
by: bobd314 | last post by:
Currently, I am having a problem replacing the value of a input box with something else using the innerHTML thing. Right now I have something going <script type="text/javascript"><!-- function...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
1
by: huzheng001 | last post by:
I have develop a on-line dictionary website, http://www.stardict.org I meet a problem: Here is two lines of js codes: document.getElementById("wordlist").innerHTML = "";...
9
by: pamelafluente | last post by:
Hi It's 2 days I am struggling with 1 stupid line of code. I have the following function which works if I do not do the dynamic code insert: document.body.innerHTML += "<form name='form1'...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.