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> Link to comment https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/ 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()? Link to comment https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/#findComment-826184 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? Link to comment https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/#findComment-826225 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. Link to comment https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/#findComment-826262 Share on other sites More sharing options...
aooga Posted May 4, 2009 Author Share Posted May 4, 2009 Thank you!!!! Link to comment https://forums.phpfreaks.com/topic/156840-whats-wrong-with-this-javascript-code/#findComment-826267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.