php - JS events. Only one of the event handler function is being called? -
hi everybody!
have problem , have no idea how fix it!
i have simple html page button:
<input type = "submit" value = "ok" onclick="f1()"/>
i have script on page:
<script> function f2() { var firstbtn = document.createelement("button"); firstbtn.appendchild(document.createtextnode("firstbtn")); document.body.appendchild(firstbtn); var xmlhttp = createrequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { var response = xmlhttp.responsetext; document.body.innerhtml += response; } } firstbtn.onclick = function() { alert("inside f2 onclick"); xmlhttp.open("get","test.php",true); xmlhttp.send(); } } function f3() { var secondbtn = document.createelement("button"); secondbtn.appendchild(document.createtextnode("secondbtn")); document.body.appendchild(secondbtn); var xmlhttp = createrequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { var response = xmlhttp.responsetext; document.body.innerhtml += response; } } secondbtn.onclick = function() { alert("inside f3 onclick"); xmlhttp.open("get","test.php",true); xmlhttp.send(); } } function f1() { f2(); f3(); } function createrequest() { var xmlhttp; if (window.xmlhttprequest) xmlhttp=new xmlhttprequest();// code ie7+, firefox, chrome, opera, safari else xmlhttp=new activexobject("microsoft.xmlhttp");// code ie6, ie5 return xmlhttp; } </script>
so now, when click 1 of buttons (firstbtn or secondbtn), 1 not responding click event! know why?
as @vishwanath said (see comments question), when doing
document.body.innerhtml += response;
i'm destroying , recreating content of page therefore i'm losing events.
Comments
Post a Comment