I am looking form a nested data to a DOM element. Something like this
$("div").data( "test", {
first: 16,
last: "pizza!"
});
Here is my effort to make similar kind of format. But RefAttrValue variable is not behaving as variable.
for (var i = 0; i < reference.attributes.length; i++) {
var RefAttrValue = (reference.attributes.item(i).name);
$(tableCaption).data("referenceData").RefAttrValue = reference.attributes.item(i).value;
}
Here the Values of the “reference” is a XML. Which has value like below or can contain any similar XML.
<facilityreference Name="qtitem_fac" AttrTable='table1' colVal='table1colValue'></facilityreference>
I want to set up the data() format in this below format “Dynamically”.
$("div").data("test",{ Name: "qtitem_fac", last: "table1colValue" });
Please help.
As far as I understand, you need something like this:
In your code I see few problems:
First: If you do nothing like
$(tableCaption).data("referenceData", {...})before you run your code, line$(tableCaption).data("referenceData")will return undefined and that will cause an exception.If you have a field name in a variable (
RefAttrValuein your case) you need to do something like$(tableCaption).data("referenceData")[RefAttrValue]. Such notation is a synonym, ifRefAttrValue = "Name", to$(tableCaption).data("referenceData").NameDoing this
$(tableCaption).data("referenceData").RefAttrValueyou simply access property of referenceData with nameRefAttrValue.