for(... in ...)
Can be used to iterate over fields in a data set or rows in detail table. Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in.
for(variable
in object
) { ... }
Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
Examples
This script iterates over field names in the current record and adds them to a paragraph.
for(var i in record.fields){
results.after("<p>" + i + "</p>");
}
Selector | Matched element | Matched element after script execution |
---|---|---|
#test | <h1 id="test">Fields</h1> | <h1 id="test">Fields</h1> <p>first</p> <p>last</p> <p>email</p> |
This script iterates over fields in the current record, retrieving their values. Then it adds the values to a paragraph.
for(var i in record.fields){
results.after("<p>" + record.fields[i] + "</p>");
}
Selector | Matched element | Matched element after script execution |
---|---|---|
#test | <h1 id="test">Fields</h1> | <h1 id="test">Fields</h1> <p>Peter</p> <p>Parker</p> <p>pparker@localhost.com</p> |
This script iterates over rows in a detail table and adds the contents of the 'country' field to a paragraph.
for(var i in record.tables['countries']) {
results.after("<p>" + record.tables['countries'][i].fields['country'] + "</p>");
}
Selector | Matched element | Matched element after script execution |
---|---|---|
#countries | <h1 id="countries">Countries</h1> | <h1 id="countries">Countries</h1> <p>The Netherlands</p> <p>Canada</p> <p>Australia</p> |
This script iterates over rows in a detail table and adds the contents of the 'ItemID2' field to an option. The <option> tag defines an option in a select list in an HTML form.
for(var i in record.tables['detail']) {
var str = record.tables['detail'][i].fields["ItemID2"];
results.append("<option value='" + str + "'>" + str + "</option>");
}