Twitter like countdown characters for open ends

Since in Confirmit there’s no option to limit the characters in a text area, here’s a simple script that will show a nice countdown, as you type, twitter like, and will also limit the number of characters to a max

Or course, you can set up a custom validation that will check for the open end length in confirmit validation tab, the advantage of this script is that is very visual, and the respondent doesn’t have to hit the next button and get an error message back.

First make sure you include the jQuery library in your skin header.

The script below works with one textarea per page, but it can be easily customized to work with more

In the open end instruction tab, place the following script:



To explain a bit what the script does, the limit here is set to 120 characters, and you can change that,

The span will be updated as you type, when the limit is reached, will truncate the text and re-set it back to the text area.

Also when it gets to the 0 remaining, I'm adding the "errorquestion" class to the countdown span, that's the default confirmit error CSS style class for the error messages, so the respondents will get one more hint to see when they have reached the limit

Happy coding everyone!

Confirmit dynamic questions with a twist

We know Confirmit has the option to trigger a second question (or more) based on a previous question answer, you may find this called dynamic questions.
You need to use a page object and add the questions inside then choose “triggers” on the ones you need to have them triggered.
You can define complex logic and all the other functionality such as text piping, question masking etc. can be controlled by the trigger function, so you can take advantage of all these.

That’s a pretty cool feature that allows you to chain some logic and have it working on the same page.

The downside of this is that the positioning on the screen of the second question is not always easy.
Also the logic is only evaluated when the page is submitted (when the respondent clicks the Next button), and sometime is hard to follow when the logic is complex.

I will present a different alternative for the triggers (or dynamic questions):

The set up will be the same, 2 questions on the same page, the second will be triggered by answering an option from the first question. In this example I’m using 2 multiple questions.
The second question will show up right under the option that triggers it, so its answers will nicely align under it.

In the answer label we need to use for triggering the second question will insert an empty div tag with a class, let`s call it “willTrigger”, this will help us identify which one is the one we will need to check if is clicked (checked) or not.

In my example I have this answer label to trigger the second question

Product ingredients

Here`s the script that makes all this possible, again I`m using jQuery, so don`t forget to include a reference to jQuery library in your skin:

$(function(){
    //the fieldset_q8m below is the second question fieldset name, so if your question is q11 that will change to fieldset_q11
    var trigered = $('#fieldset_q8m').parents('.questionarea');
    trigered.addClass("hidden1").removeClass('questionarea').hide();
    //add an empty tdr/td
    $('.willTrigger').closest('tr').after('<tr><td colspan=2>');
    //and populate with the hidden div
    $('.willTrigger').parents('tr').next('tr').find('td').html(trigered);
    //test input change, will also hide the error message if the answer was de-selected
    $('.willTrigger').closest('tr').find('input').change(function(){
        $(this).is(':checked')?trigered.show():trigered.hide(), $('.error_msg').hide();
    });

    // here will bind a bit of logic checking if the second question was answered
    $('form').bind('submit', function(){
        if($('.willTrigger').closest('tr').find('input').prop('checked') && !trigered.find('input:checked').length>0){
            $('.error_msg').show();
            return false;
        }
        else {
            return true;
        }
    });
});

I also created a custom style in the confirmit css styles, named “hidden1”, I’m adding to the triggered question

.hidden1{
    margin-left:10px;
}

so that its answers will be nicely aligned under, creating the impression of being part of the first question answer, like an inline answer list

Will also create some logic to check if the second question is answered before the form is submitted, so will need to create an error message like

Please select at least one ingredient

and also define this in the confirmit style list

.error_msg{
    display:none;
}

or just add the “display:none” inline


The “errorquestion” class is the default error style already defined in confrmit

The erro message div add it in the first question “instruction” tab, the script can also be placed there.

Here’s a fiddle example to see how it works, click on the “Product ingredients” and try to click next without giving any other answers there, see how the script detects the missing answers and reacts.

http://jsfiddle.net/confirmitexpert/rvPCK/embedded/result/

Do you often use the Confirmit default capabilities, or like to create everything from scratch, and have it fully customized?

Let respondent change the questionnaire language on the fly

After I published the last post I was thinking if the language example I picked was relevant or not, in fact, there’s a simpler way for an user to change the current language, in real time, without so much complex code and option changes I described in the previous post.

Here’s an easier way to let the user change the language.

Everything happens in the theme (main theme)
There`s no need to change any of the options from the “survey Settings”, link security like “unencrypted QID” or allow changing the previous answers etc, this will work with the default settings.

First we need a button or similar element,

<div class="navigationarea">
<input class="navbutton confirmit-nav" onclick="changeLanguage()" alt="" value="Switch to ^CurrentLang()=='9'?'French':'English'^" size="20" type="button"/>
</div>

As you can notice, the onclick event calls this changeLanguage() function, and its content is below:

function changeLanguage(){
    var lang= "^CurrentLang()^";
    location.href="^GetRespondentUrl()^"+String.fromCharCode(38)+(lang=="9"?"l=12":"l=9");
}

So far so good, everything works, but still there’s this screen

You have already completed parts of the questionnaire.
Press OK to continue.

Every time you click the button you get this screen (with the default settings)

To make the process smoother we will detect that page, “Internal_Continue_survey_on_entry_text”, hide its content, (please make sure you use the same wrapper class for the div that surrounds your content if not please replace it with your own), and submit the form

$(function(){
   if( $("#Internal_Continue_survey_on_entry_text").length>0 ) {
       $(".wrapper").hide();
       document.ctlform.submit();
   }
});

So let’s add everything together, please place this let’s say at the end of your main theme, right after the navigation component. In the layout tab use the code editor, not the WYSIWYG.
Don’t forget to include the jQuery library in the external js
“External Javascript Url” like https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js

All added together below


<div class="navigationarea">
<input class="navbutton confirmit-nav" onclick="changeLanguage()" alt="" value="Switch to ^CurrentLang()=='9'?'French':'English'^" size="20" type="button"/>
</div>

Easy real-time survey interaction

Today’s subject is about a nice technique that allows you, I mean the respondent, to interact with almost anything from inside the survey in a very transparent way, whenever they want and are allowed, of course.

You may find this helpful if you do iPad/mobile surveys on-site as well.

This can go from changing the theme UI (don’t think about setting up any cookies though!), the language, till changing or going into a specific module then come back and complete the rest.

The example I picked is to let the respondent change the questionnaire language in real time (you need to have the language and the corresponding texts added already in Confirmit) and this can be extended to anything else you may think about.

The set up is easy, and as you can see in the image below we need some variables:

setup folder

“language” is the background “language”, and let’s say we’ve got English and French, precodes 9 and 12.
“l” we’re using to control the current language, this reflects in the language the questionnaire is displayed in.
the “set language” script has the following content:

if(! f('l').toBoolean()) f('l').set(f('language').get());

the “qleft” is an open end that stores the survey current question ID (is updated every time the page loads)
and below is the “scripts” node content, I’ve added it here to quickly see how “qleft” gets updated with the questionID, the function setQLeft being called from the theme

function setQLeft(){
  var currentForm=CurrentForm();
  
  if (currentForm!="lch") f('qleft').set(currentForm);
}

function langDisplay(){
  return f('l').get()=='9'?"French":"English";
}

Notice the test if currentForm!=”lch” so we don’t set it if this question, otherwise we end up in a loop

The langDisplay helps in the survey theme, returning the language string you want to switch into

next there’s a condition node set to false, meaning this will be never executed in the normal flow

“lch” is an info question type with the content as follows

wait...

The next script node is setting the new language, and then redirects them where they left

f('l').get()=='9'?f('l').set('12'):f('l').set('9');

Redirect(GetRespondentUrl()+"&__qid="+f('qleft').get(),true );

and lastly, the script to redirect back where they left

if ( f('q1').toBoolean() && f('qleft').toBoolean() ) {
  Redirect(  GetRespondentUrl() + "&__qid=" + f('qleft').get(), true);
}

Next step is to add and call some of these function from inside the theme since we need to make them visible and allow the respondents interact with them somehow.

main theme layout custom change language button

As you can notice -red square- setQLeft is called every time the page loads, and once it’s executed it writes the questionID (currentForm) so the last question id if there are multiple per page

the second – green square- is a javascript function that will change the location url and sends him to the specific questionid lch (language change)
Tips String.fromCharCode(38), I used this instead of simple adding the “&” character, Confirmit always replaces the “&” with “& amp;”

and third – blue square- a button that runs at on click the function above, and also notice its value is defined as a function call from langDisplay that returns the language you want to change into

^setQLeft()^


Last you need to make some changes in the Survey Settings tab
“General Options”
Uncheck “Disable unencrypted QID request parameter (recommended to prevent respondents from tampering with the survey urls to skip to specific questions)”
“Web Options”
Check “Allow respondents to change their original answers”

The reason I used the question id as the solution to jump between questions was because you can control exactly where and how.

I learned you can do something similar now using the blocks and calling them when you jump, but didn’t have the opportunity to test everything, maybe you guys are already using this approach?

Redirect to a different project or web application, and capture data back

Here is a technique I used in various projects I was working on, where I had to link to a different project, modules or third party applications, pass some data and collect other data back.

Start by creating a script node with the content below:

var applicationUrl="the_application_url";
var theRespondentUrl=HttpUtility.UrlEncode(GetRespondentUrl()+"&__qid=backFromApp");
//or since Confirmit15 GetRespondentUrl("backFromApp");
var x,y="";//here some variables and values, you can take those from other questions as you wish
var additionalVariables="&x="+x+"&y="+y;
Redirect(applicationUrl+"?linkBack="+theRespondentUrl+additionalVariables,true);

Next, add an info question, with the ID backFromApp, and here is the place where we expect to have the respondent returned,

<input type="hidden" name="dataCaptured" id="dataCaptured">
<script type="text/javascript">
function getUrlVariables(){ 
  var variablesUrl = [], variableNameValue; 
   var variablePair = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for(var i = 0; i < variablePair.length; i++)    { 
       variableNameValue = variablePair[i].split('='); 
       variablesUrl.push(variableNameValue[0]);  
       variablesUrl[variableNameValue[0]] = variableNameValue[1]; 
   }
  return variablesUrl;
}

document.ctlform.dataCaptured.value=getUrlVariables()[ 'oneInParticular'];//store data in hidden form element, dataCaptured
document.ctlform.submit();//go to the next question
</script>

Please make sure you use the right variable name, since it’s case sensitive.

A script node follows the info question above, and stores the data back in Confirmit, let’s say you have an open end with the id “dataCaptured”

f('dataCaptured').set(Request('dataCaptured'));

On the other end (in the external app, or even Confirmit project) you only need to capture the link, store it, append the data and redirect the respondent back.