Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    help removing div layer from screen

    I add a DIV layer to my page using the appendChild() function.. here's the code:

    Code:
    div = document.createElement("DIV");
    div.id = "layer1";
    div.style.height = "200px";
    div.style.width = "140px";
    div.style.background = "#CCCCCC";
    div.style.display = "block";
    document.appendChild(div);
    how do I then remove it from the screen?! I've tried

    document.getElementById("layer1").style.display = "none";

    but that has no effect. I was thinking something like removeChild() ?? But cant seem to find any documentation or tutorials on this sort of thing.

    any help is much appreciated.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Have you confirmed that the div is really added?

    You need to add it to the body or to any child element of it.

    document.body.appendChild(div);

    You can use any of the two methods to hide the div but if you want to show it later, it's easier to use

    document.getElementById("layer1").style.display = "none";

    and then setting it to 'block' later to display it.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    yeah, I do a check:

    if(document.getElementById("layer1"))
    {
    //remove it
    }

    so the code checks - and if there is the DIV layer, it removes it. Ive checked the code, it does show the div layer and adds the HTML code to the body. I wondered if there was a way of simply removing the element with the id "layer1" from the HTML code?

    Ive tried to "hide" the div layer, and it didnt work....

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by homerUK
    I was thinking something like removeChild() ?? But cant seem to find any documentation or tutorials on this sort of thing.

    MSDN

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by homerUK
    yeah, I do a check:

    if(document.getElementById("layer1"))
    {
    //remove it
    }

    so the code checks - and if there is the DIV layer, it removes it. Ive checked the code, it does show the div layer and adds the HTML code to the body. I wondered if there was a way of simply removing the element with the id "layer1" from the HTML code?

    Ive tried to "hide" the div layer, and it didnt work....
    What does this say?

    alert(document.getElementById("layer1"))
    if(document.getElementById("layer1")){
    //...
    }

    Also check for any error in the Javascript Console (NS/Moz) or in the exclamation icon (IE) in the status bar.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •