Find an attribute's parent element

D

Deken

In Infopath 2003, in JScript, is it possible to determine an attribute's
parent element? I need to be able to update the value of a sibling attribute
when the first attribute is changed, and I can find no way to climb the tree
from the attribute to its containing element.

I would think that this would work:
/**********************************************/
function OnAfterChange(eventObj)
{
var curNode = eventObj.Source;
var found = false;
while (! found) {
if (curNode.nodeTypeString == "element"){
var modflag = curNode.getAttribute("modflag");
if (modflag != null) { // null is missing
if( modflag != "true" ) {
curNode.setAttribute("modflag","true");
}
found = true;
}
}
curNode = curNode.parentNode;
if (curNode == null) {
found = true; // pseudo found - reached top
}
}
return;
}
/**********************************************/
except that when curNode.nodeTypeString == "attribute", curNode.parentNode
is null.

TIA for any assistance available!

Deken
 
G

Greg Collins [InfoPath MVP]

In your situation you would do the following:

var parent = curNode.selectSingleNode("..");

This works despite parentNode being null.
 
D

Deken

THANK YOU!

First I tried
/****************************************************/
function OnAfterChange(eventObj)
{
var curNode = eventObj.Source;
var found = false;
while (! found) {
if (curNode.nodeTypeString == "element"){
var modflag = curNode.getAttribute("modflag");
if (modflag != null) { // null is missing
if( modflag != "true" ) {
curNode.setAttribute("modflag","true");
}
found = true;
}
}
curNode = curNode.selectSingleNode("..");
if (curNode == null) {
found = true; // pseudo found - reached top
}
}
return;
}
/****************************************************/

However, that results in an error: "MSXML5.DLL: A text node child of an
attribute may not be passed as the context node for an XSLT transform or an
XPath query."
(eventObj starts out as the text node child of the 'status' attribute, once
for a "Delete" operation to get rid of the existing value, a second time for
an "Insert" operation to add the new value)

SOLUTION:
The code that works is
/****************************************************/
function OnAfterChange(eventObj)
{
var curNode = eventObj.Source;
var found = false;
while (! found) {
if (curNode.nodeTypeString == "element"){
var modflag = curNode.getAttribute("modflag");
if (modflag != null) {
if( modflag != "true" ) {
curNode.setAttribute("modflag","true");
}
found = true;
}
}
else if (curNode.nodeTypeString == "text"){
curNode = curNode.parentNode;
}
else if (curNode.nodeTypeString == "attribute"){
curNode = curNode.selectSingleNode("..");
}
if (curNode == null) {
found = true; // pseudo found - reached top or can't climb higher due to
object model idiosyncracies
}
}
return;
}
/****************************************************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top