Last post Mar 12, 2018 08:18 PM by lewis886
Mar 09, 2018 07:07 PM|lewis886|LINK
I've got an update to make to an html template. Basically I need to grab a couple values, do a calculation, and update a field with the result.
The page was set up as an HTML template. The template is grabbed, and the data that will be populated into the template is embedded into the page as XML, then the data is populated throughout the page using the "datafld" attribute of "div" elements. Here
you can see the top where the XML is embedded, and then below that I give you an example of how the data is populated into the page.
<?xml version='1.0' ?>
<description>Description of Item</description>
<table border="0" width="100%" cellspacing="3" cellpadding="1" datasrc="#XmlData">
<td width="30%" align="left" class="Detail">Quantity: </td>
<td><div id="qtyID" datafld="quantity"></div></td>
The problem is, when I grab the div where the datafld is being used, it simply grabs the datafld attribute (ex above, "quantity"). I can't grab the actual value "3". So, my question is, is there a good way to grab the actual value that the datafld is referencing
using jQuery? Or, is there a good way to grab the tag value from the embedded XML section directly, using jQuery? I can't change anything about how the XML gets embedded (such as adding attributes to the tags or anything).
The closest I have come is using the code below, which grabs the datafld property, meaning it returns "quantity".
var Quantity = $("#qtyID").attr("datafld");
Mar 11, 2018 10:04 PM|bruce (sqlwork.com)|LINK
the xml will loaded as plain dom elements (of unknown types).
var q = $('#XmlData Header subheader quantity').text();
I would have expected the datafld attribute to look like an xpath, say: "/Header/subheader/quantity", so you know how to access (not having to know the root path). in which case a simple mapping would have worked:
var $xml = $('#XmlData');
// get xml value
var path = $("#qtyID").attr("datafld").split('/').join(' ');
var value = $xml.find(path);
Mar 12, 2018 09:23 AM|AngelinaJolie|LINK
. I can't grab the actual value "3". So, my question is, is there a good way to grab the actual value that the datafld is referencing using jQuery?
Sir, In jquery, please refer to this simple example
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );
alert( $title.text() );
alert RSS Title
For one xml object, $xml.find(
For array xml, $(xml).find('Book').each(
and if your xml string is in a div please use this way to get the xml string from a div
Welcome to back if met any question :)
With regards, Angelina Jolie
Mar 12, 2018 08:18 PM|lewis886|LINK
It ended up being a little tougher, but you basically had the answer. I ended up having to strip out line breaks and other characters that got introduced from the embedding, and once I did that I didn't even have to parse it out as XML.
var text = $('#XmlData');
var text2 = text.innerHTML;
var xml = text2.replace(/[\r\n]/g, '');
var xml2 = xml.replace(/\s/g, '');
$xml = $(xml2),
$quantity = $xml.find("quantity");
var QuantityNum = parseInt($quantity.text());
Thanks a lot. :)