Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript XML set node value

Tags:

javascript

xml

How can I set a value to a node

var testVal = "some value"
var XML = document.createElement("div");
var Node = document.createElement("ROOT NODE");
var node1 = document.createElement("TESTING-ONE");

node1.appendChild( testVal );                                   
Node.appendChild(node1);
XML.appendChild(Node);

alert(XML.innerHTML);
like image 228
Doc Holiday Avatar asked Nov 30 '25 04:11

Doc Holiday


1 Answers

Use createTextNode:

node1.appendChild(document.createTextNode(testVal));

Side note: element names cannot contain spaces, so the following is an error:

var Node = document.createElement("ROOT NODE");
like image 181
Wayne Avatar answered Dec 02 '25 19:12

Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!