Jump to content

What's wrong with this javascript code?


aooga

Recommended Posts

<script type="text/javascript">

function a() {

document.getElementById('2').innerHTML = "<p onclick='b()'>a</p>";

}

function b() {

document.getElementById('2').innerHTML = "hello"; // this doesn't work

document.getElementById('3').innerHTML = "hello"; // this does

}

</script>

 

<p id='1' onclick='a()'>1</p>

<p id='2' onclick='a()'>2</p>

<p id='3' onclick='a()'>3</p>

Link to comment
https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/
Share on other sites

I've made the code as small as possible to isolate the problem, but this should be enough to demonstrate it.

When I click on 1, 2 or 3 then 2 becomes a. Then when I click on a, 3 becomes hello, but b does not become hello. I'm at a loss as to why.

Btw: if function a() {

document.getElementById('2').innerHTML = "<p onclick='b()'>a</p>";

document.getElementById('2').innerHTML = "hello";

}

this would work. It's only if

document.getElementById('2').innerHTML = "hello";

is in b() that it doesn't. I'm REALLY curious about this, what am I missing?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.