Lua Scripting Resources

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language.


Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. Read-only scripts will continue to function; you will just be prevented from editing. The exact date on this is to be determined; we will send notifications well ahead of time.


 Go to our Legacy Scripting Documentation.

While SurveyGizmo is one of the most flexible survey tools around, we get requests for customizations that are not available out of the box. This is where JavaScript and Custom Scripting can save the day. If you have scripting chops you can use the JavaScript action or the Custom Scripting action to achieve the survey of your dreams.


[article("bodfy")]

You may have noticed that when setting up your survey page to randomly show a set of questions, Text/Instruction fields are not included! But of course, there's always JavaScript!

See the script in action in an example survey!

OR

Add a survey with this script and setup to your account.

The Script and Setup

If you have multiple text/instruction fields and need to randomly show some specific number of those to the respondent, just add the following script to a JavaScript action on the same page!

$(document).ready(function() {
    var numToShow = 1; //Change this number to how many fields we need to show

    /*-----Don't touch anything below this line-----*/
    var textEls = $('.sg-type-instruction');
    var randIndices = [];

    for (var i = 0; i < numToShow; i++) {
        var randIndex = Math.floor(Math.random() * textEls.length);
        do {
            randIndex = Math.floor(Math.random() * textEls.length);
        } while (randIndices.indexOf(randIndex) !== -1);

        randIndices.push(randIndex);
    }
    //console.log(randIndices);
    textEls.each(function(i, el) {
        if (randIndices.indexOf(i) === -1) {
            el.remove();
            //console.log('set for removal: #' + i);
        }
    });
});

Required Customizations

Specify the number of elements you want to show in the highlighted portion of the script. In the above script, 1 random Text/Instruction will be shown to the respondent.

Record which text element was shown

If you'd like to also know which element was shown to the respondent, the following JavaScript will populate a Textbox question with which text/instruction/s are displayed.

Check it out in an example survey!

OR

Add a survey with this script to your account!
$(document).ready(function() {
	var numToShow = 2; //Change this number to how many fields we need to show
  
  /*-----Don't touch anything below this line-----*/
	var textEls = $('.sg-type-instruction');
	var randIndices = [];
  var shownArr = [];
  var shownStr;
	
	for (var i = 0; i < numToShow; i++) {
		var randIndex = Math.floor(Math.random() * textEls.length);
		do {
			randIndex = Math.floor(Math.random() * textEls.length);
		} while (randIndices.indexOf(randIndex) !== -1);
		
		randIndices.push(randIndex);
	}
	//console.log(randIndices);
	textEls.each(function(i, el) {
		if (randIndices.indexOf(i) === -1) {
			$(this).remove();
			//console.log('set for removal: #' + i);
		} else {
			var classArr = $(this).attr('class').split(' ');
			//Fix for Preview
			if (classArr.indexOf('sg-cc-hook') !== -1) classArr.splice(classArr.indexOf('sg-cc-hook'), 1);
			var shownClass = classArr[classArr.length - 1];
			shownArr.push(shownClass);
		}
	});
  	shownStr = shownArr.join(',');
  	$('.shown').find('input[type="text"]').val(shownStr);
});

Required Customizations

In this script you will also need to make the following customizations: