How to add zeros at the end of the number entered by participant? | XM Community
Solved

How to add zeros at the end of the number entered by participant?

  • 30 January 2019
  • 1 reply
  • 17 views

One of the questions we regularly ask in our survey is about sales. Respondents are asked to answer in $ THOUSANDS. For instance, if their sales was $150000, they should enter 150 only. I would like to create a box which is pre-populated with 000, which I should be hanging at the end of any number that they enter:

___ 000 // before replying
150 000 // after replying

Any ideas, how I can achieve that?

Thanks
icon

Best answer by fleb 30 January 2019, 22:13

View original

1 reply

Userlevel 5
Badge +6
Hi @yesplease ,
1) You could simply put your zeros into the textbox. Just right-click on the box, then click "Add Default choices" and type there your zeros. In this case, the user will be allowed to rewrite your zeros.

2) You could put your zeros right after the text box like this:
Qualtrics.SurveyEngine.addOnload(function()
{
var questionId = this.questionId;
var choiceInputs = $$('#'+this.questionId + ' .ChoiceStructure input');
for (var i=0; i < choiceInputs.length; i++)
{var choiceInput = choiceInputs[i]; try
{if (choiceInput && choiceInput.parentNode)
{choiceInput.parentNode.appendChild(QBuilder('span',{},'000'));
}
}
catch(e)
{
}
}
});
3) You could put zeros into the textbox like in the first case, but check each time user releases a key. In case there are not enough zeros at the end of the string, they'll be added. You can also remove zeros from the beginning of the string. I wrote a function for this. Unfortunatelly, I haven't found how to modify text-box content using JavaScript and therefore I wasn't able to implement it for a text-entry question. Here is a version for user-defined text field.
<body>


<input type='text' id='zeronumber' value='000'/>

<script>

document.addEventListener("keyup", zOnChange);

function zOnChange()
{
var ele = document.getElementById('zeronumber');
var value = ele.value;
var n = value.length;

ele.value = value.replace(/^0+/, '');

if( value.substring(n-3,n)!='000' )
{
if(value.substring(n-2,n)=='00')
{ele.value = value + '0';}
else
{if(value.substring(n-1,n)=='0')
ele.value = value + '00';
else {ele.value = value + '000';}
}

}
}
</script>
</body>

Leave a Reply