AllisonC
AllisonC

Reputation: 3100

Why is setting the innerHTML not working?

My xml document is this:

<Order>
  <size width="16.5" height="19.5">
    <width>16.5</width>
    <height>19.5</height>
  </size>
  <type>photo</type>
  </overlay>
  <Mats></Mats>
  <Openings></Openings>
  <Moulding></Moulding>
  <Glass></Glass>
</Order>

I am trying to fill in the values of the Mats tag. Here is my javascript code:

function common_get_order_xml()
{
  var parser = new DOMParser(); //create a new DOMParser
  var doc = parser.parseFromString(common_get_common_order_xml(), "application/xml"); //convert the string to xml
  console.log(doc); //outputs what is above
  //doc.getElementsByTagName('Mats').innerHTML = mattes_get_mattes_xml().replace("<Mats>", "").replace("</Mats>", ""); //add the mattes
  doc.getElementsByTagName('Mats').innerHTML = "TEST";
  doc.getElementsByTagName('Openings').innerHTML = mattes_get_openings_xml(); //Add the openings
  doc.getElementsByTagName('Moulding').innerHTML = moulding_get_moulding_xml(); //Add the moulding
  doc.getElementsByTagName('Glass').innerHTML = glazing_get_glass_xml(); //Add the moulding
  var serializer = new XMLSerializer(); //create a new XMLSerializer
  common_order_xml = serializer.serializeToString(doc); //convert the xml back to a string
  return doc;
}

But when I output doc, the Mats innerHTML does not get set to "TEST"

Upvotes: 0

Views: 108

Answers (1)

Oracle
Oracle

Reputation: 318

When using JavaScript with XML you shouldn't read and set the innerHTML, Chrome and Firefox try to allow you to do this but you can get some weird behavior.

Instead of accessing something.innerHTML try using somthing.firstChild.data.

Upvotes: 1

Related Questions