aooga Posted May 4, 2009 Share Posted May 4, 2009 <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> Quote Link to comment Share on other sites More sharing options...
gnawz Posted May 4, 2009 Share Posted May 4, 2009 post more code so we can get it. Where's the fi=unction b()? Quote Link to comment Share on other sites More sharing options...
aooga Posted May 4, 2009 Author Share Posted May 4, 2009 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? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 It does work. When you click on "a" generated by the JavaScript, you change 2 and 3 to hello, but the problem is that clicking "a" on 2 also triggers 2's a() onclick, so you change it back. It happens so fast you just don't see it. Quote Link to comment Share on other sites More sharing options...
aooga Posted May 4, 2009 Author Share Posted May 4, 2009 Thank you!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.