Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Input default answers (numerical values) to array (numbers) with text input

More
8 years 11 months ago #118453 by Sev
Hello,
Is there a way to input default numerical values as answers of an Array (Numbers) with text inputs?

Something like this? Trying to input default values of 0 for each answer cell... (although this one does not work)
Code:
<script type="text/javascript">
  $(document).ready(function() {
    $('#answer442200X4X2S1_Cost1').val('0');
    $('#answer442200X4X2S2_Cost1').val('0');
    $('#answer442200X4X2S3_Cost1').val('0');
    $('#answer442200X4X2S1_Cost2').val('0');
    $('#answer442200X4X2S2_Cost2').val('0');
    $('#answer442200X4X2S3_Cost2').val('0');
  });
</script>

Thank you in advance for any tip!
The topic has been locked.
More
8 years 11 months ago #118504 by Sev
Actually it works, sorry, I had a filter on that did not let me see the answer.
Also I did not think I would be able to write a readily working script :P
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #118524 by tpartner
I think you want to test to make sure the input is empty before loading the default:
Code:
<script type="text/javascript">
  $(document).ready(function() {
    if($('#answer442200X4X2S1_Cost1').val() == '') {
      $('#answer442200X4X2S1_Cost1').val('0');
    }
    if($('#answer442200X4X2S2_Cost1').val() == '') {
      $('#answer442200X4X2S2_Cost1').val('0');
    }
    if($('#answer442200X4X2S3_Cost1').val() == '') {
      $('#answer442200X4X2S3_Cost1').val('0');
    }
    if($('#answer442200X4X2S1_Cost2').val() == '') {
      $('#answer442200X4X2S1_Cost2').val('0');
    }
    if($('#answer442200X4X2S2_Cost2').val() == '') {
      $('#answer442200X4X2S2_Cost2').val('0');
    }
    if($('#answer442200X4X2S3_Cost2').val() == '') {
      $('#answer442200X4X2S3_Cost2').val('0');
    }
  });
</script>

Or, if you want to set defaults for all inputs in the question, simplify it with a loop:
Code:
<script type="text/javascript">
  $(document).ready(function() {
    // Loop through all inputs in this question
    $('#question{QID} input[type="text"]').each(function(i) {
      if($(this).val() == '') {
        $(this).val('0');
      }
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
8 years 11 months ago #118556 by Sev
That's very useful, thank you very much for your reply!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose