innerHTML and event handlers

T

Tim Fooy

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
 
M

Mike

Tim Fooy said:
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.
 
W

William Morris

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
 
T

Tim Fooy

Hi all,
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
 
T

Tim Fooy

"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
 
T

Tim Fooy

Tim Fooy said:
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
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
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top