I have a div and inside that div I have a few radio buttons. Both have id's. I tried to delete the radio buttons by using document.getElementById('divid').innerHTML = ''; ,but it giving me an error. Why is this and how do I correctly delete elements from a page without javascript giving me an error? Thanks. ~imozeb
Which error does it give? Maybe you want to change innerHTML of the div before it is loaded. Try this: <script type="text/javascript"> window.onload = function(){ document.getElementById("divid").innerHTML = ""; } </script> Code (markup):
Or else try DOM methods ... Say your 2 radio buttons have id attributes of 'rb1' and 'rb2', and their parent div has id of 'divid', then do this: var delete1 = document.getElementById("rb1"); var delete2 = document.getElementById("rb2"); var parentelement = document.getElementById("divid"); // or could instead do: var parentelement = delete1.parentNode; parentelement.removeChild(delete1); parentelement.removeChild(delete2); Code (markup):
It's still not working. I wish it gave an error message but it doesn't, instead I can't click the error dialog box to make it pop up. The javascript still works but it says there is an error without showing me where the error is. Help!!! I've commented out most of my code and I have found out my problem. Now I just need to figure out how to fix it! This is my problem. I have two radio buttons. When one is checked it shows one thing. When the other one is checked it shows another thing. The problem arises when I try to use .innerHTML to populate the div with an imput box. If I comment out the .innerHTML of the div then it works fine. But if I have the .innerHTML of the div it locks up (no error report ). Can anyone tell me how to fix this? And finally if I comment out everything except the setInterval and one .innerHTML that only runs if it hasn't been run once (using if(document.getElementById('divid')){}else{//.innerHTML here}it doesn't display an error, it just wont let me click anything on the browser window except the address bar? I'm so confused? Thanks! ~imozeb
use document.createElement and (element).appendChild to create your input box and add it to your div.