How do I delete elements?

Discussion in 'JavaScript' started by Imozeb, Apr 5, 2010.

  1. #1
    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
     
    Imozeb, Apr 5, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    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):
     
    s_ruben, Apr 6, 2010 IP
  3. FilmFiddler

    FilmFiddler Greenhorn

    Messages:
    54
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    23
    #3
    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):
     
    FilmFiddler, Apr 6, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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 :)
     
    Last edited: Apr 6, 2010
    Imozeb, Apr 6, 2010 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    use document.createElement and (element).appendChild to create your input box and add it to your div.
     
    camjohnson95, Apr 6, 2010 IP