Programmatically retrieve the value of another field in the same row of a repeating table
Use the Source property of the DataDOMEvent object in the OnAfterChange event of a field in a repeating table to retrieve the value of another field in the same row of the repeating table using JScript.
You have added an OnAfterChange event handler on the FirstName field of a Repeating Table and would like to retrieve the value of the LastName field that is in the same row as the FirstName field.
Your Repeating Table looks as follows when you preview the InfoPath form:
The XML for a row in the Repeating Table looks like:
<Player ID="10" FirstName="Chipper" LastName="Jones" No="10" Position="3B" />
Use the Source property of the eventObj object that is being passed to the OnAfterChange event to retrieve the value for LastName in the OnAfterChange event handler for the FirstName field:
var lastName = "";
lastName = eventObj.Source.parentNode.selectSingleNode("../@LastName").text;
The Source property of the DataDOMEvent object returns a reference to the XML Document Object Model (DOM) where the data validation event is occurring.
If you change the text in the FirstName field in the Repeating Table, this action will trigger the OnAfterChange event on this field, and eventObj.Source in the OnAfterChange event handler will return a #text node. You can check this by adding the following code to the OnAfterChange event handler:
So to get to the node for the FirstName field attribute you would have to use
since the FirstName node will be the parent of the #text node being changed.
Once you have the node for the FirstName field attribute, it is only a matter of using an XPath expression to navigate upwards to retrieve the parent node of FirstName and then back downwards to get to the node for the LastName field attribute as in the following XPath expression:
Suppose just for a minute that the XML for your Repeating Table does not consist of field attributes, but of field elements. For example, the XML for a row in your Repeating Table looks like:
In this case you would have to use the following code to retrieve the value of LastName in the OnAfterChange event handler for the FirstName field:
var lastName = "";
lastName = eventObj.Source.parentNode.selectSingleNode("../LastName").text;
If you have added a Data Connection to your InfoPath form and named it Players, you can retrieve and display the entire XML for this secondary data source using the following code:
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- Auto-fill a repeating table with data from a SharePoint list when a row is inserted
- Programmatically execute code when a repeating table row is inserted or deleted
- Programmatically retrieve the value of an InfoPath form field using .NET code
- How to retrieve field values in InfoPath
- How do I retrieve the value of an InfoPath field through code?