Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing xml document object with javascript

Status
Not open for further replies.

mashadt

Instructor
Apr 23, 2005
33
ZA
Here is a question about manipulating a document object with javascript:

To access the value of an attribute in an XML document via javascript in a seperate html document, I can retrieve the attribute using the W3C XML DOM -

The XML looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<notes xmlns:xlink="
<stuff classed="working">

</stuff>
</notes>


The javascript looks like this:


function getAtt()

{

var el;
var strOutput = "";

el = dataXML.documentElement.getElementsByTagName("stuff");
//dataXML is a reference to the XML document object

strOutput = el[0].attributes.getNamedItem("classed").value;

document.getElementById("here").innerHTML = strOutput;
//to display the attribute value in the html document

}


What I would like to know is if it is possible to change the value of that attribute in the XML document object using javascript in my html page? Can I use the replaceChild(newChild, oldChild) method of the Node object, or something similar? So far I have not managed to achieve this.

Is the attribute "classed" a child of the "stuff" element?
 
It is read/write.
[tt]
function setAtt(svalue) {
var el;
el = dataXML.documentElement.getElementsByTagName("stuff");

//Method [1]
//el[0].setAttribute("classed", svalue);
//or Method [2]
//el[0].selectSingleNode("@classed").value=svalue;
//or Method [3]
el[0].attributes.getNamedItem("classed").value=svalue;
}
[/tt]
Just call it like this.

[tt] setAtt("newclass");[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top