Remove div?

U

UKuser

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
 
E

Evertjan.

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';
 
U

UKuser

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


a div is not within javascrip but within html.


do you use Ajax or redisplay the page?


Why not:

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

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
 
E

Evertjan.

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

WHY did you not answer this highly relevant question?

[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.
 
U

UKuser

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

WHY did you not answer this highly relevant question?

[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.

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
 
E

Evertjan.

UKuser wrote on 16 aug 2007 in comp.lang.javascript:
UKuser wrote on 16 aug 2007 in comp.lang.javascript:
On 16 Aug, 11:19, "Evertjan." <[email protected]>
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.
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.

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]
 
M

Marcos Toledo

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
 
E

Evertjan.

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.
 
B

blaine

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);
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top