Retrieving Text List Meta Data with Javascript

Text lists can supply additional "Meta Data" that is accessible through scripting during the survey process.
Reference Guide

What is a Text List?

Text lists allow very large non-choice data to be selected via a search facility during the survey process. This is perfect for a selection of suburbs or postal codes or large lists of products or anything where the list of data is very big and is only used for confirmation purposes.

For information on how to create and incorporate Text Lists in your survey Click here

Text List "Meta Data"

Text lists can be extended with "Meta Data" that can be added to the list. This is very useful for users with access to custom scripting as they can obtain the meta data in script and use it for many purposes, such as selecting question choices based on the information in the meta data.

Here is an example of a Text List spreadsheet showing suburb, state and post code data for Australia along with the "MetaData" column which is broken down and includes latitude and longitude data. The meta data in this example is in JSON format and can be natively used by scripting.
Text List Meta Data

Retrieving the "Meta Data" with Javascript

Let's take a look at how we can pull apart and capture specific Meta Data.

The following example demonstrates accessing and parsing the meta data of a chosen Australian post code. The post code value and suburb name will be pulled out separately from the Meta Data and set into other questions on the page. To access the questions with scripting, ensure each question as a unique Question Access Code.

First create the survey structure:
  • 1. Add a Single Line Text question to your survey. Enable the Text List Options and choose a Text List. This example will be using a default system Text List.
    Text List Meta Data Enable Text List
  • 2. Add another Single Line Text and a Numeric question to the page. The suburb name and post code values will be captured in these questions. The data captured in these additional questions can then also be used for Data Piping in subsequent survey pages.
    Text List Meta Data Survey Questions
  • 3. Add a Javascript script to the survey page, set the script to the "Execute Javascript when Next or Submit Buttons are Pressed" scripting event.
    Text List Meta Data Scripting Event
Retrieving the Text List meta data with script is very easy once the data has been parsed using the JSON.parse() Javascript method:
Text List Meta Data Parsed
You can then simply access and set the data you need from the parsed object:
///Get the survey page questions
var oQTextList = wscScripting.getQuestionByDataPipingCode('AUSPOSTCODES');
var oQSuburb_Name = wscScripting.getQuestionByDataPipingCode('SUBURB_NAME');
var oQPostcode = wscScripting.getQuestionByDataPipingCode('POSTCODE');

///Get the text list metadata using the question identity
var oMetaData = $('#' + oQTextList.identity + '_metadata').val();

///Parse the text list meta data
var oParsedMetaData = JSON.parse(oMetaData);

///Get the postcode from the metadata, convert the value from a string data type to an integer
var nPostCode = parseFloat(oParsedMetaData.PostalCode);

///Set the postcode integer into the numeric question
wscScripting.setValue(oQPostcode, nPostCode);

///Set the suburb name into the single line text question
wscScripting.setValue(oQSuburb_Name, oParsedMetaData.Suburb);

The script above will set the numeric post code and suburb text into the two additional questions when the next/submit button is clicked:
Text List Meta Data Example