how do I remove a node?
-
I am using the DOM along with DHTML to dynamically manipulate html... According to a certain event I need to remove a node, but I cant find a method for removing or deleting nodes. I am aware of the replaceNode() method but this requires another node to replace the old one with which is not good for me... Any ideas?:confused: Thank you |---------------| | theJazzyBrain | |---------------|
-
I am using the DOM along with DHTML to dynamically manipulate html... According to a certain event I need to remove a node, but I cant find a method for removing or deleting nodes. I am aware of the replaceNode() method but this requires another node to replace the old one with which is not good for me... Any ideas?:confused: Thank you |---------------| | theJazzyBrain | |---------------|
Using DHTML, you can always parse out the HTML tag of the element you wish to delete from the parent's innerHTML and set it back. EX:
function DeleteElement(oElem) {
var oParent = oElem.parentElement;
var cParentHTML = StringReplace(oParent.innerHTML,"\r\n","");
var cDeleteHTML = StringReplace(oElem.outerHTML,"\r\n","");var iIndex = cParentHTML.indexOf(cDeleteHTML);
if (iIndex > -1)
{
oParent.innerHTML = cParentHTML.substr(0,iIndex) + cParentHTML.substr(iIndex+cDeleteHTML.length);
}}
function StringReplace(cSearch, cFind, cReplace) {
var iPos = cSearch.indexOf(cFind);
while (iPos > -1)
{
cSearch = cSearch.substr(0,iPos) + cReplace + cSearch.substr(iPos+cFind.length);
iPos = cSearch.indexOf(cFind);
}return cSearch;
}Hope this helps (and i just typed this in, so don't rely on it working).
-
Using DHTML, you can always parse out the HTML tag of the element you wish to delete from the parent's innerHTML and set it back. EX:
function DeleteElement(oElem) {
var oParent = oElem.parentElement;
var cParentHTML = StringReplace(oParent.innerHTML,"\r\n","");
var cDeleteHTML = StringReplace(oElem.outerHTML,"\r\n","");var iIndex = cParentHTML.indexOf(cDeleteHTML);
if (iIndex > -1)
{
oParent.innerHTML = cParentHTML.substr(0,iIndex) + cParentHTML.substr(iIndex+cDeleteHTML.length);
}}
function StringReplace(cSearch, cFind, cReplace) {
var iPos = cSearch.indexOf(cFind);
while (iPos > -1)
{
cSearch = cSearch.substr(0,iPos) + cReplace + cSearch.substr(iPos+cFind.length);
iPos = cSearch.indexOf(cFind);
}return cSearch;
}Hope this helps (and i just typed this in, so don't rely on it working).
Sorry, forgot to sign in before posting...;P onwards and upwards...
-
Using DHTML, you can always parse out the HTML tag of the element you wish to delete from the parent's innerHTML and set it back. EX:
function DeleteElement(oElem) {
var oParent = oElem.parentElement;
var cParentHTML = StringReplace(oParent.innerHTML,"\r\n","");
var cDeleteHTML = StringReplace(oElem.outerHTML,"\r\n","");var iIndex = cParentHTML.indexOf(cDeleteHTML);
if (iIndex > -1)
{
oParent.innerHTML = cParentHTML.substr(0,iIndex) + cParentHTML.substr(iIndex+cDeleteHTML.length);
}}
function StringReplace(cSearch, cFind, cReplace) {
var iPos = cSearch.indexOf(cFind);
while (iPos > -1)
{
cSearch = cSearch.substr(0,iPos) + cReplace + cSearch.substr(iPos+cFind.length);
iPos = cSearch.indexOf(cFind);
}return cSearch;
}Hope this helps (and i just typed this in, so don't rely on it working).
Kinda expensive solution :) DOM1 defines the
removeChild
method, so why not use it:var parent = document.getElementById("anelement");
var child = document.getElementById("anotherelement");
parent.removeChild(child);IE DOM0 implements the
removeNode
method:var child = document.getElementById("anotherelement");
child.removeNode(true);