Controlling Side-by-Side input type | XM Community

Controlling Side-by-Side input type

  • 3 February 2023
  • 2 replies
  • 100 views

Userlevel 7
Badge +38

I have a side-by-side with three rows and three columns. Columns 1 and 2 need to collect integers, but column 3 needs to allow numbers (with decimals). I modified a bunch of code I found on here and know which line is helping but also not quite right for my needs. Any help modifying it would be great. My survey is already live and none of the stakeholders mentioned this requirement. However looking at the first three submissions I see the need for this.
The last row of code is where I am having issues but wanted to provide as much context of my code and variables as possible.
Qualtrics.SurveyEngine.addOnReady(function()
{
//laod the library
var $jq = jQuery.noConflict(); //always keep this
//disable editing first and third row
//first row pulls in historical information
//row three calcualtes totals for columns 1 & 3 (column 2 only shows a box for row 2)
$jq('.SBS1 input:eq(0)').attr("disabled",true);
$jq('.SBS1 input:eq(2)').attr("disabled",true);
$jq('.SBS3 input:eq(0)').attr("disabled",true);
$jq('.SBS3 input:eq(2)').attr("disabled",true);

//hide first and third row of second column (total visitors)
var qid=this.questionId;
$('QR~'+qid+'#3~1~1~TEXT').up('td').childElements().invoke('hide');
$('QR~'+qid+'#3~10~1~TEXT').up('td').childElements().invoke('hide');

//seetting the value 0 for all boxes if blank
$jq('.SBS1 input').each(function(){ //column 1
$jq(this).val(getVal($jq(this).val()));
});
$jq('.SBS2 input').each(function(){ //column 2
$jq(this).val(getVal($jq(this).val()));
});
$jq('.SBS3 input').each(function(){ //column 3
$jq(this).val(getVal($jq(this).val()));
});

//change the text boxes to numeric
//this works great for columns 1 & 2 that are counting people and needs to be integers
//column 3 counts units and those could be down to quarter hours it is wiping out the decimals when saving the data
//I think I need to change parseInt to Number but I don't want it to be a Number for all columns
function getVal(val){var num = parseInt(val); if(isNAN(num)) {num=0;} return num;};


2 replies

Userlevel 7
Badge +27

I would add another function:
function getFloat(val){var num = parseFloat(val); if(isNAN(num)) {num=0;} return num;};
Then change the function call for the third column:
$jq('.SBS3 input').each(function(){ //column 3
$jq(this).val(getFloat($jq(this).val()));
});


Userlevel 7
Badge +38

TomG thank you so much for always being so helpful! This worked out perfectly, as always.

Leave a Reply