Automatically add new lines | XM Community
Solved

Automatically add new lines

  • 8 November 2018
  • 18 replies
  • 359 views

Userlevel 1
I want to ask a text entry question where I don't know how many answers respondents will want to list. E.g., list all the footballers that come to your mind. How can I get Qualtrics to automatically add another line, if a respondent has used up the number of text entry fields that first show up?
icon

Best answer by Anonymous 8 November 2018, 13:06

View original

18 replies

Hello @automaticadd ,

Solution 1: You can go for loop and merge

Solution 2: If you can specify the maximum number that the user can enter in the footballers kown question. Then in the next question you can have form question type and can apply display logic at option level. Eg: the display logic at option 3 will be if number is less than or equal to three.
Userlevel 7
Badge +27
@automaticadd,

Or, if you can write JS, you can have a text form with the maximum number of footballers and hide all but the first input field. Then as the respondent enters something in a field, unhide the next empty field.
Userlevel 1
@Shashi thanks for your answer. I tried loop and merge (solution 1) and perhaps I didn't understand it properly but it seemed that all versions always referred to a previous answer. I actually need additional text entry fields within the same question if a respondent uses all text entry fields I provide.
I have the same problem with solution 2; they would need to indicate first how many they plan to list. But I want them to rather go ahead intuitively, until they can't come up with any more..
Nonetheless, thanks for your answer.
Userlevel 1
@TomG Thank you for taking the time. Your answer sounds promising. I don't want to limit the answer choices so I would have to go for an unrealistic high number of options. My problem, however, is that I have never written a javascript.
Is there any other way of doing this? Is there no function that would automatically add additional entry fields if a respondent uses all the fields available. I feel like I've come across this many times in filling out surveys?!
Userlevel 7
Badge +27
> @automaticadd said:
> Is there any other way of doing this? Is there no function that would automatically add additional entry fields if a respondent uses all the fields available. I feel like I've come across this many times in filling out surveys?!

No, the only way to do it is with JavaScript.
Userlevel 1
Ok, thanks!
Userlevel 1
Hi again

I realised there is the option of implementing a display logic on choice level. I used a text entry question and set the number of Form Fields to 30. The first three form fields are unconditional and should show up no matter what. Within this question XY, e.g. for the fourth choice/text entry field I have the display logic: "Display this Choice only if the following condition is met: 'Question' 'Q XY' 'Choice 2' 'Is Not Empty'.

When I preview the survey and put some characters in Choice 2, I don't get an additional form field. Is there an obvious mistake in my logic?

Thanks in advance
Userlevel 7
Badge +27
Yes, choice display logic is applied on the server before the page gets sent to the browser. The only way to do it dynamically is with JS.
Userlevel 1
@TomG once again, thanks for your quick answer. I have no experience with writing in javascript. Do you think it is possible to learn JS within a reasonable amount of time to be able to write such a "feature" that would dynamically unhide form fields? If yes, can you recommend where/what I should start with?
Userlevel 7
Badge +27
@automaticdd - This will be a bit beyond basic JS. Here is a resource for learning html, css, JS, and jQuery (each is a prerequisite for the next): https://www.w3schools.com/
Userlevel 1
@TomG Thanks!
Userlevel 1
Badge +8
Hi @automaticadd I hope that your account is still active and you get this! I'm just wondering if you found a solution to this problem outside of JavaScript - I need to do the same thing ("list all classes you've taken" which could be 1 or 100), and while I could probably write the JavaScript, it's definitely beyond the capabilities of some of my collaborators to understand. It seems like just leaving 50 blank lines is the best option, which I can just put on its own page, but that's going to make a mess out of the response table.
Userlevel 1
Hi @riceball. I was lucky enough to have a friend write the javascript for me because there really does not seem to be another solution. I'm happy to share the code but I'm not sure if that is what you are after?
Userlevel 1
Badge +8
Hi @automaticadd, I was hoping there was a non-code way so I could share with my non-technically minded coworkers how to do it themselves, but doing it any way is better than not doing it at all! I'd really appreciate if you shared to code tidbit. That would save me an incredible amount of work trying to figure it out. Thank you!!
Userlevel 1
sure. You can find the code below. it is for the addOnload section. the way it works is that I created 100 text entry fields. with the code below, 15 are initially visible, the rest is hidden. whenever you click or tab in to the last field visible, an additional field appears. hope this helps, best of luck.

{
jQuery("#"+this.questionId+" .InputText").focus( function() {
var isInputText = document.activeElement instanceof HTMLInputElement && document.activeElement.type == 'text';

console.log(isInputText);

if (isInputText)
{
tbody2 = document.activeElement.parentNode.parentNode.nextSibling.nextSibling.show()//style.display = 'inline-block';
}
});

var initialFields = 15;

var choices = this.getChoiceContainer();

//choices.hide();

//alert(choices);

var tbody = choices.getElementsByTagName('tbody');
//alert(tbody[0].childNodes.length);

childs = tbody[0].childNodes;


for (i=(initialFields*2); i <childs.length; i++)
{
if (childs[i].style) {childs[i].style.display = 'none';}
}


this.questionclick = function(event,element)
{
var isInputText = element instanceof HTMLInputElement && element.type == 'text';


if (isInputText)
{
tbody2 = element.parentNode.parentNode.nextSibling.nextSibling.show()//style.display = 'inline-block';
}
}
});
Hi, I would love to use the code above to create multiple single lines and appear more as needed. I am asking respondents to list all their membership organizations. But, I don't know JS. I tried to use the code above by copy-pasting it into my "add JavaScript...". It didn't work. Could someone help me? Thanks in advance!
Userlevel 1
Hi

Unfortunately, I'm not an expert either. Here's a few things that come to mind, which you can try:
- ensure that the question type is set to text entry and the text type is set to form
- make sure you have 17+ text entry fields (I had 100)
- paste it in the right place (within curly brackets after "addOnload(function()"); leave the rest of the script unchanged
- the code i pasted as a comment somehow was split in two; use both parts

Best of luck
OMG! It worked! Thank you @automaticadd!!

Leave a Reply