470,636 Members | 1,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

javascript not executed from the innerHTML

Hi,
I am trying to understand why this code does not work as expected.
FF shows the message twice; IE doesn't show the message at all.
What would be the right way to get the message displayed only once?
Please, help, thanks...
Jano
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script language="javascript">
function test_of_innerhtml(){
document.getElementById('n1').innerHTML='<strong
onmouseout="javascript:alert(\'Hi\');">hi in</strong>';
}
</script>
</head>
<body>
<div id='n1' onmouseover="javascript:test_of_innerhtml();">hi</div>
<br />
<a
href="javascript:alert(document.getElementById('n1 ').innerHTML);">Show
innerHTML</a>
</body>
</html>

Dec 8 '06 #1
1 1763
ASM
Janooo a écrit :
Hi,
I am trying to understand why this code does not work as expected.
FF shows the message twice; IE doesn't show the message at all.
What would be the right way to get the message displayed only once?
Please, help, thanks...
http://stephane.moriaux.perso.wanado...nerHTML_danger

Try this and probably will you see what happens :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<style type="text/css">
div, strong { border:1px solid red; padding: 2px 8px; }
</style><head>
<title>Untitled Page</title>
<script language="javascript">
var i = j = 0;
function test_of_innerhtml(){
document.getElementById('n1').innerHTML='<strong
onmouseout="javascript:alert(\'Hi_\'+i);i++">hi in '+j+'<\/strong>';
j++;
}
</script>
</head>
<body>
<div id='n1' onmouseover="javascript:test_of_innerhtml();">hell o</div>
<br />
<a
href="javascript:alert(document.getElementById('n1 ').innerHTML);">Show
innerHTML</a>
</body>
</html>

When mouse leaves tag strong it leaves too the div reinserting a new
strong leaved as soon as created (2 tags strong in one).
To avoid double alert, probably with a counter could it be possible ?

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by William Pierce | last post: by
22 posts views Thread by Christopher Nelson | last post: by
8 posts views Thread by Pratik Patel | last post: by
3 posts views Thread by jackson.rayne | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.