Remove div?

Discussion in 'Javascript' started by UKuser, Aug 16, 2007.

  1. UKuser

    UKuser Guest

    How can I remove a div with a known name/id within javascript?

    The reason I need to do this, is I have a div called <div id='test'>
    which displays some database fields. AJAX/PHP/MYSQL updates the
    database and then redisplays the fields within the same div. However
    doing this can remove from the DOM all of the fields/input boxes from
    the DIV and thus produce a non object/null value error. Not sure if
    simply pasting the fields into a new DIV is the best idea.

    Thanks

    A
    UKuser, Aug 16, 2007
    #1
    1. Advertising

  2. UKuser

    Evertjan. Guest

    UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    > How can I remove a div with a known name/id within javascript?


    a div is not within javascrip but within html.

    > The reason I need to do this, is I have a div called <div id='test'>
    > which displays some database fields. AJAX/PHP/MYSQL updates the
    > database and then redisplays the fields within the same div.


    do you use Ajax or redisplay the page?

    > However
    > doing this can remove from the DOM all of the fields/input boxes from
    > the DIV and thus produce a non object/null value error. Not sure if
    > simply pasting the fields into a new DIV is the best idea.


    Why not:

    document.getElementByIs('test').style.dispay = 'none';


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 16, 2007
    #2
    1. Advertising

  3. UKuser

    UKuser Guest

    On 16 Aug, 11:19, "Evertjan." <> wrote:
    > UKuser wrote on 16 aug 2007 in comp.lang.javascript:
    >
    > > How can I remove a div with a known name/id within javascript?

    >
    > a div is not within javascrip but within html.
    >
    > > The reason I need to do this, is I have a div called <div id='test'>
    > > which displays some database fields. AJAX/PHP/MYSQL updates the
    > > database and then redisplays the fields within the same div.

    >
    > do you use Ajax or redisplay the page?
    >
    > > However
    > > doing this can remove from the DOM all of the fields/input boxes from
    > > the DIV and thus produce a non object/null value error. Not sure if
    > > simply pasting the fields into a new DIV is the best idea.

    >
    > Why not:
    >
    > document.getElementByIs('test').style.dispay = 'none';
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    Is there a way to actually remove it though? I.e. something to do with
    removeChild? I need it to fully go, not just be invisible.

    Thanks

    A
    UKuser, Aug 16, 2007
    #3
  4. UKuser

    Evertjan. Guest

    UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    > On 16 Aug, 11:19, "Evertjan." <> wrote:
    >> UKuser wrote on 16 aug 2007 in comp.lang.javascript:
    >>
    >> > How can I remove a div with a known name/id within javascript?

    >>
    >> a div is not within javascrip but within html.
    >>
    >> > The reason I need to do this, is I have a div called <div
    >> > id='test'> which displays some database fields. AJAX/PHP/MYSQL
    >> > updates the database and then redisplays the fields within the same
    >> > div.

    >>
    >> do you use Ajax or redisplay the page?


    WHY did you not answer this highly relevant question?

    >> > However
    >> > doing this can remove from the DOM all of the fields/input boxes
    >> > from the DIV and thus produce a non object/null value error. Not
    >> > sure if simply pasting the fields into a new DIV is the best idea.

    >>
    >> Why not:
    >>
    >> document.getElementByIs('test').style.display = 'none';


    [please do not quote signatures on usenet. Removed.]

    > Is there a way to actually remove it though?


    What would be the benefit?

    > I need it to fully go, not just be invisible.


    [by "need" I hope you mean "want".]

    The solution I gave you was NOT making the Div invisible,
    that would have been:

    document.getElementByIs('test').style.visibility = 'hidden';

    but to remove it from view.

    > I.e. something to do with removeChild?


    What would be the benefit of deleting that DOM element?
    It would only take execution time.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 16, 2007
    #4
  5. UKuser

    UKuser Guest

    On 16 Aug, 13:13, "Evertjan." <> wrote:
    > UKuser wrote on 16 aug 2007 in comp.lang.javascript:
    >
    > > On 16 Aug, 11:19, "Evertjan." <> wrote:
    > >> UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    >
    > >> > How can I remove a div with a known name/id within javascript?

    >
    > >> a div is not within javascrip but within html.

    >
    > >> > The reason I need to do this, is I have a div called <div
    > >> > id='test'> which displays some database fields. AJAX/PHP/MYSQL
    > >> > updates the database and then redisplays the fields within the same
    > >> > div.

    >
    > >> do you use Ajax or redisplay the page?

    >
    > WHY did you not answer this highly relevant question?
    >
    > >> > However
    > >> > doing this can remove from the DOM all of the fields/input boxes
    > >> > from the DIV and thus produce a non object/null value error. Not
    > >> > sure if simply pasting the fields into a new DIV is the best idea.

    >
    > >> Why not:

    >
    > >> document.getElementByIs('test').style.display = 'none';

    >
    > [please do not quote signatures on usenet. Removed.]
    >
    > > Is there a way to actually remove it though?

    >
    > What would be the benefit?
    >
    > > I need it to fully go, not just be invisible.

    >
    > [by "need" I hope you mean "want".]
    >
    > The solution I gave you was NOT making the Div invisible,
    > that would have been:
    >
    > document.getElementByIs('test').style.visibility = 'hidden';
    >
    > but to remove it from view.
    >
    > > I.e. something to do with removeChild?

    >
    > What would be the benefit of deleting that DOM element?
    > It would only take execution time.
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    1) I am using AJAX to refill a DIV
    2) The benefit of removing the DIV would be to remove confusion on re-
    extracting information from the redisplayed/new form
    UKuser, Aug 16, 2007
    #5
  6. UKuser

    Evertjan. Guest

    UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    > On 16 Aug, 13:13, "Evertjan." <> wrote:
    >> UKuser wrote on 16 aug 2007 in comp.lang.javascript:
    >>
    >> > On 16 Aug, 11:19, "Evertjan." <>
    >> > wrote:
    >> >> UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    >>
    >> >> > How can I remove a div with a known name/id within javascript?

    >>
    >> >> a div is not within javascrip but within html.

    >>
    >> >> > The reason I need to do this, is I have a div called <div
    >> >> > id='test'> which displays some database fields. AJAX/PHP/MYSQL
    >> >> > updates the database and then redisplays the fields within the
    >> >> > same div.

    >>
    >> >> do you use Ajax or redisplay the page?

    >>
    >> WHY did you not answer this highly relevant question?
    >>
    >> >> > However
    >> >> > doing this can remove from the DOM all of the fields/input boxes
    >> >> > from the DIV and thus produce a non object/null value error. Not
    >> >> > sure if simply pasting the fields into a new DIV is the best
    >> >> > idea.

    >>
    >> >> Why not:

    >>
    >> >> document.getElementByIs('test').style.display = 'none';

    >>
    >> [please do not quote signatures on usenet. Removed.]
    >>
    >> > Is there a way to actually remove it though?

    >>
    >> What would be the benefit?
    >>
    >> > I need it to fully go, not just be invisible.

    >>
    >> [by "need" I hope you mean "want".]
    >>
    >> The solution I gave you was NOT making the Div invisible,
    >> that would have been:
    >>
    >> document.getElementByIs('test').style.visibility = 'hidden';
    >>
    >> but to remove it from view.
    >>
    >> > I.e. something to do with removeChild?

    >>
    >> What would be the benefit of deleting that DOM element?
    >> It would only take execution time.
    >>
    >> --
    >> Evertjan.
    >> The Netherlands.
    >> (Please change the x'es to dots in my emailaddress)


    AGAIN:
    [please do not quote signatures on usenet.]

    > 1) I am using AJAX to refill a DIV


    Please answer right under the relevant quote.

    > 2) The benefit of removing the DIV would be to remove confusion on re-
    > extracting information from the redisplayed/new form


    Whose confusion?

    You could replace the form, meseems.
    [other children are not disturbed this way]

    var myNewForm = ....;
    var myForm = document.forms['myForm'];
    var myContainer = myForm.parentNode;
    myContainer.replaceChild(myNewForm,myForm);


    [not tested]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 16, 2007
    #6
  7. Hi UKuser,

    The best way I know for removing a child from the DOM is to go through
    the parent and then removing the child.

    function removeElement(element) {
    return element.parentNode.removeChild(element);
    }

    Hope it helps. If someone has a better approach, I'll be glad to learn
    it.

    Cheers,
    Toledo
    Marcos Toledo, Aug 16, 2007
    #7
  8. UKuser

    Evertjan. Guest

    Marcos Toledo wrote on 16 aug 2007 in comp.lang.javascript:

    [please always quote on usenet, this is not email]

    > The best way I know for removing a child from the DOM is to go through
    > the parent and then removing the child.
    >
    > function removeElement(element) {
    > return element.parentNode.removeChild(element);
    >}


    No it is not always(!) the best,
    since the subsequent [re]placing of another child
    could disturb the order of children.

    replaceChild() is better.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 16, 2007
    #8
  9. UKuser

    Guest

    On Aug 16, 4:16 am, UKuser <> wrote:
    > How can I remove a div with a known name/id within javascript?
    >
    > The reason I need to do this, is I have a div called <div id='test'>
    > which displays some database fields. AJAX/PHP/MYSQL updates the
    > database and then redisplays the fields within the same div. However
    > doing this can remove from the DOM all of the fields/input boxes from
    > the DIV and thus produce a non object/null value error. Not sure if
    > simply pasting the fields into a new DIV is the best idea.
    >
    > Thanks
    >
    > A


    Remember when you are removing an element you will want to make sure
    all it's child elements are removed as well.

    EX.
    function clearContent(elementName){
    var element = document.getElementById(element);
    while (element.hasChildNodes()){
    element.removeChild(element.lastChild);
    }
    }
    , Aug 16, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,372
    Henrik
    Aug 10, 2005
  2. tshad
    Replies:
    6
    Views:
    21,462
    tshad
    Aug 8, 2006
  3. K Viltersten

    <div ... /> and <div ...></div>

    K Viltersten, Mar 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    745
  4. Dwayne Madsen

    Q: Div A inside Div B is larger than Div B

    Dwayne Madsen, Jun 1, 2005, in forum: Javascript
    Replies:
    1
    Views:
    186
    David Dorward
    Jun 1, 2005
  5. mscir
    Replies:
    3
    Views:
    307
    Martin Honnen
    Jun 26, 2005
Loading...

Share This Page