eb1 eb1 - 2 months ago 4
Javascript Question

xml nodeValue string interpreted as number

I'm parsing through an XML document using jquery, and seem to be having some problems with the

nodeValue
of an attribute. I've got the following element:

<chapter number="1" formattednumber="1." />


...where the attribute
formattednumber
is the string that will eventually be displayed in the UI. That's fine and well, until we look at:

element.attributes.item("formattednumber").nodeValue


Which returns a
number
instead of a string value (i.e., a
1
instead of a
"1."
-- interpreting the dot as a decimal point and removing it). Is there any way I can get the text value out of the attribute instead?

Answer

To get the attribute from an XML node as a string, you can use getAttribute()

element.getAttribute('formattednumber')