Qualtrics: embedded data field javascript

2,176 views
Skip to first unread message

dharmahound

unread,
Aug 14, 2012, 6:39:40 PM8/14/12
to scrip...@googlegroups.com
Hi, I'm having a hard time finding the right syntax to refer to embedded data fields (EDF) in javascript.  

For example, I set up an EDF in survey flow called "tester", and i give it the value "hello_world".

Then I create a question item in the actual survey, and in the javascript section I try to call the field using:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place Your Javascript Below This Line*/

  var new_word "${e://Field/tester}";
  alert('the word is: '+new_word);
    
});

Then I run the survey in preview mode, and the alert box pops up but only shows "the word is: ".  Since it doesn't just spit out "the word is ${e://..." it makes me think that qualtrics accepted the reference to the Field/ category, but couldn't pull up the value, for some reason.

I've tried a few different variations for feeding in the embedded data value, nothing seems to work, and there isn't very clear API from qualtrics for how to do this sort of thing.  


Ultimately I also want to be able to call the array of all EDFs in a survey flow, so i can loop through the entire set and choose one based on its name.  

for example, if i have three EDF: test-1, test-2, test-3, i'd like to be able to loop through all of them, using a similar method to the conventional document.getElementsByTagName, parse out the digits in the name of each field and select the one that ends in the number 3.  then i'd want to extract the data from test-3 and display it on an actual question item in the qualtrics survey.

can anyone help me with this?


thanks,
andrew

dharmahound

unread,
Aug 14, 2012, 7:22:58 PM8/14/12
to scrip...@googlegroups.com
Problem half-solved:

I figured out why I couldn't call the EDF in javascript - the syntax was fine, I just had the EDF placed after the question item in the survey flow.  

I'm still not sure how to call the array of all EDFs though.  I would like to run a for-loop that looks something like this:


var elements_array = [syntax to call array of EDFs named ];

for (var i = 0; i < edf_array.length; i++) {

       alert("This EDF field's name is" + elements_array[i].name + "and its value is" + elements_array[i].value);

}

I know this must be possible, but Qualtrics support team isn't very well versed in the full Javascript API, and the API offered online barely scratches the surface of its total functionality.

Thomas Schubert

unread,
Aug 15, 2012, 6:32:29 AM8/15/12
to scrip...@googlegroups.com
Hi Andrew,

first - when you say API, do you refer to Qualtrics' question API, linked to at http://www.qualtrics.com/university/researchsuite/coders-corner/javascript ?

re your question - I don't think it's possible the way you want it. The way I understand it, when you refer to embedded data fields in an added javascript, this reference is replaced by the value of the embedded data field by the server. Thus, when I refer to ${e://Field/mycode}, the moment the page is served, this is replaced by the content of mycode. You see this when you view the source of the page when actually taking the survey. Thus, at runtime, your script has no way to actually see the different Fields at e:// - only the server sees them when rendering the page.

But there might be a workaround. Perhaps you can create first a piece of javascript that, when executed by the page, creates the array that you want to loop through later?

something like:

var myEDFArray = new Array();

myEDFArray[0] = "${e://Field/mycode1}";
myEDFArray[1] = "${e://Field/mycode2}";
myEDFArray[2] = "${e://Field/mycode3}";

then this array is available at runtime.

awkward and cumbersome, I know, because you need to hard code all the field names.

hope this helps,
thomas
-- 
thomas schubert
department of psychology, university of oslo 
schu...@igroup.org
http://www.igroup.org/schubert/

dharmahound

unread,
Aug 15, 2012, 5:45:59 PM8/15/12
to scrip...@googlegroups.com
Hi Thomas, 

I think I've figured out a way to do what I want to do, which isn't as modular as I would like but it still saves me from hand coding all the EDFs into an array ahead of time.  Thanks for your response, I really appreciate it!

Best,
Andrew

On Tuesday, August 14, 2012 6:39:40 PM UTC-4, dharmahound wrote:

Andrew Long

unread,
Nov 29, 2012, 9:38:17 AM11/29/12
to scrip...@googlegroups.com
Probably a little late on this, but by looking at the set/addEmbeddedData methods, it doesn't look too difficult. EDFs are just hidden input elements, and their ids all start with "ED~". So all you have to do is construct a CSS selector to find them and use the already implemented jQuery to look them up (or I suppose you don't have to use jQuery, but if its already there its just so easy).
 
getEmbeddedData:function(key)
{
   var fieldName='ED~'+key;
   if($(fieldName))
   {
      return $(fieldName).value;
   }
}
setEmbeddedData:function(key,value)
{
   var fieldName='ED~'+key;
   if($(fieldName))
   {
      $(fieldName).value=value;
   }
   else
   {
      $('Header').appendChild(QBuilder('input', {type:'hidden', id:fieldName, name:fieldName, value:value}));
   }
}


On Tuesday, August 14, 2012 6:39:40 PM UTC-4, dharmahound wrote:
Reply all
Reply to author
Forward
0 new messages