How to setup autofill for constant sum fields or piping for autofill / “must sum to” fields | XM Community
Skip to main content
Solved

How to setup autofill for constant sum fields or piping for autofill / “must sum to” fields

  • August 14, 2020
  • 4 replies
  • 455 views

Forum|alt.badge.img

Dear Community,
I am looking to auto fill fields in constant sum question to a total of 100.
For example in a two field constant sum question, when one filed is filled as 30 then next one should auto fill as 70 (attaching visual as well).
qualtrics.JPG
Could you please help with with this functionality (JS code?)
Many thanks!

Best answer by SurajK

Hi Abi ,
Use the below code, assuming you have 2 statements, the below code will work vice a versa i.e. if user change the value in 2nd input then first will get auto fill.
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery('input[type="text"]').eq(0).change(function(){
var secondinput = jQuery('input[type="text"]').eq(1);
if(jQuery(this).val() > 0)
{
secondinput.val(100-jQuery(this).val())
}
});

jQuery('input[type="text"]').eq(1).change(function(){
var firstinput = jQuery('input[type="text"]').eq(0);
if(jQuery(this).val() > 0)
{
firstinput.val(100-jQuery(this).val())
}
});

});

4 replies

SurajK
QPN Level 3 ●●●
Forum|alt.badge.img+4
  • QPN Level 3 ●●●
  • Answer
  • August 17, 2020

Hi Abi ,
Use the below code, assuming you have 2 statements, the below code will work vice a versa i.e. if user change the value in 2nd input then first will get auto fill.
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery('input[type="text"]').eq(0).change(function(){
var secondinput = jQuery('input[type="text"]').eq(1);
if(jQuery(this).val() > 0)
{
secondinput.val(100-jQuery(this).val())
}
});

jQuery('input[type="text"]').eq(1).change(function(){
var firstinput = jQuery('input[type="text"]').eq(0);
if(jQuery(this).val() > 0)
{
firstinput.val(100-jQuery(this).val())
}
});

});


Forum|alt.badge.img
  • Author
  • August 17, 2020

SurajK This is awesome! Works like a charm- thanks man :)
Also, just wanted to see how this could be modified for a side-by-side table with numeric entries. Please see the image (yellow highlight row) for reference:
image.png


SurajK
QPN Level 3 ●●●
Forum|alt.badge.img+4
  • QPN Level 3 ●●●
  • August 18, 2020

https://www.qualtrics.com/community/discussion/comment/29176#Comment_29176Use this code, this is as per the screenshot given above.
Qualtrics.SurveyEngine.addOnReady(function()
{
//First column
jQuery('.c4 input[type="text"]').change(function(){
var Nonecol = jQuery('.c4:eq(5) input[type="text"]').val();
if(jQuery(this).val() > 0)
{
var curval = Number(jQuery(this).val()) + Number(Nonecol)
jQuery('.c4:eq(5) input[type="text"]').val(curval)
}
});


//Second column
jQuery('.c7 input[type="text"]').change(function(){
var Nonecol = jQuery('.c7:eq(5) input[type="text"]').val();
if(jQuery(this).val() > 0)
{
var curval = Number(jQuery(this).val()) + Number(Nonecol)
jQuery('.c7:eq(5) input[type="text"]').val(curval)
}
});


jQuery('.c8 input[type="text"]').change(function(){
var Nonecol = jQuery('.c8:eq(5) input[type="text"]').val();
if(jQuery(this).val() > 0)
{
var curval = Number(jQuery(this).val()) + Number(Nonecol)
jQuery('.c8:eq(5) input[type="text"]').val(curval)
}
});


//Third Column

jQuery('.c11 input[type="text"]').change(function(){
var Nonecol = jQuery('.c11:eq(5) input[type="text"]').val();
if(jQuery(this).val() > 0)
{
var curval = Number(jQuery(this).val()) + Number(Nonecol)
jQuery('.c11:eq(5) input[type="text"]').val(curval)
}
});


jQuery('.c12 input[type="text"]').change(function(){
var Nonecol = jQuery('.c12:eq(5) input[type="text"]').val();
if(jQuery(this).val() > 0)
{
var curval = Number(jQuery(this).val()) + Number(Nonecol)
jQuery('.c12:eq(5) input[type="text"]').val(curval)
}
});


});



Forum|alt.badge.img
  • Author
  • August 19, 2020

SurajK Thank you kind sir! :)