Populating additional text entry fields upon hitting "enter" | XM Community
Question

Populating additional text entry fields upon hitting "enter"

  • 20 June 2019
  • 6 replies
  • 24 views

I am asking respondents to think of as many examples as they can within a one minute time frame (open text entry), and would like to populate a new text entry field each time they hit 'enter'.

A previous attempt to use display logic did not succeed - any suggestions?

Thanks so much!

6 replies

Hello @Ig236 ,

You can use form field question type with all except first text entry hidden and based on enter key up event show the next text entry field.
Thank you so much, can that be achieved through the display logic? I'm still struggling to find where to define the logic you mentioned.
> @lg236 said:
> Thank you so much, can that be achieved through the display logic? I'm still struggling to find where to define the logic you mentioned.
>

JS
I attempted to resolve this using display logic within the choice selection, however, since the "in page" option does not work for choices, the additional fields still do not appear
Userlevel 7
Badge +11
@lg236 You won't be able to do this with display logic because that would only work after the question had be answered, not after a field had been inputted. As @Shashi said, you'll need to use javascript to code this.

Otherwise, I suggest you reach out to the support team and ask them to file a Feature Request. I feel like I've seen a request for this type of functionality often recently and maybe that will push it up the list faster.
Thanks - I've played around with some JS and achieved my desired functionality, however, the entries are not saving consistently across blocks. I am using the below code across 14 blocks, and in one instance blocks 3 & 7 may be blank, while the next instance blocks 3&7 are population but 2&6 are not, for example.

Any thoughts on ways to improve the way the entries are being saved?

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
// disables the next button so the user cant click it yet
var nextBtn = document.getElementById("NextButton");
nextBtn.disabled = true;

// array to store answers
var answers = new Array();
// input box handler
var txtBox = "";
// countdown timer value
var timer = 10;


// function that finds and sets the correct ID of current text box
function getID() {
var inputs = document.getElementsByTagName("input");

var regEx = new RegExp("QR~QID");

for (var i=0; i<inputs.length ; i++) {
if (regEx.test(inputs[i].id))
txtBox = inputs[i];
}
}

// call function to get text box ID before we use it
getID();

// save input box contents to answers array and clear the field after
function saveAnswer() {
answers.push(txtBox.value);
txtBox.value = "";
clearInterval(timer);
}

// function to slow execution of code by milliseconds
function sleep(time) {
var currentTime = new Date().getTime();

while (currentTime + time >= new Date().getTime()) {
}
}

// add event listener to call function saveAnswer when enter key is pressed
txtBox.addEventListener('keyup', function(e) {
if (e.keyCode == 13) {
saveAnswer();
}

});

if (!document.getElementById("info"))
alert('not found');

// set the time we count down to which is multiplied by 1000 to equal seconds
var countDownDate1 = new Date().getTime()+(timer*1000);

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
distance = countDownDate1 - now;

var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("info").innerHTML = seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
txtBox.style.display = 'none';
txtBox.value = answers;

sleep(1000);

clearInterval(x);
//document.getElementById("demo").innerHTML = "EXPIRED";
nextBtn.disabled = false;

nextBtn.click();
}
}, 1000);


startCount();
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Leave a Reply