user593029
user593029

Reputation: 521

domparser remove node not working

Below is the code snippet:

var tmpl = "<book><auth> </auth> <price> </price> </book>";
var bkTmpl  = parser.parseFromString(tmpl,'text/xml');
var bk = bkTmpl.cloneNode();

And i am removing the child 'price' as below. Both Try1 & Try2 is not working

Try1:

var bkprice = tmpl.getElementsByTagName('price')[0];
bkTmpl .documentElement.removeChild(bkprice);

Try2

var bkprice = tmpl.getElementsByTagName('price');
bkTmpl.documentElement.removeChild(bkprice);

For Try1 it throws error => "Node was not found"

For Try2 it give => Could not convert JavaS...DOMElement.removeChild]

Google few option but i see only removeChild to remove it. However when I use document.getElementById & use removeChild function it does not complain. Only reason I am using js variable (var tmpl) as I wanted to use snippet of xml as part of javascript that can be changed in future as ajax call & hence do not want to be part of HTML. I saw on firebug for both Try1 & Try2 "price" element is populating

Upvotes: 1

Views: 1392

Answers (1)

M Abbas
M Abbas

Reputation: 6479

All you have to do is replace this line:

var bkprice = tmpl.getElementsByTagName('price')[0];
bkTmpl .documentElement.removeChild(bkprice);

by

var bkprice = bkTmpl.getElementsByTagName('price')[0];
bkTmpl .documentElement.removeChild(bkprice);

getElementByTagName is a method of bkTmpl and not tmpl.

Here is an example

Upvotes: 1

Related Questions