Welcome to the LimeSurvey Community Forum

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

Array (texts) and default value change with onclick?

  • Raymond1612
  • Raymond1612's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 6 months ago #232712 by Raymond1612
Hello. I am looking for help with the following:

I have a "Array (texts)" question with numbers only for which I use an javascript to set the default value to 0:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
 
        var defaultText = '0';
 
        $('#question{QID} tr[id^="javatbd"] input:text').each(function(i) {
            if($(this).val() == '') {
                $(this).val(defaultText)
            }
        });
    });
</script>

That's working fine, but I want the default value to be cleared once the enduser selects a cell to prevent wrong input.
I am using limesurvey Version 3.15.8+190130

Thanks in advance!

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 6 months ago #232715 by tpartner
LimeSurvey version?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • Raymond1612
  • Raymond1612's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 6 months ago #232720 by Raymond1612
I am using limesurvey Version 3.15.8+190130

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 6 months ago #232727 by tpartner
This script should do the trick:

Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
 
        var defaultText = '0';
 
    var $inputs = $('#question{QID} tr[id^="javatbd"] input:text');
 
        $inputs.each(function(i) {
            if($.trim($(this).val()) == '') {
                $(this).val(defaultText);
            }
        }).on('keydown', function(e) {
      if($.trim($(this).val()) == defaultText) {
        $(this).val('');
      }
    }).on('keyup change', function(e) {
      if($.trim($(this).val()) == '') {
        $(this).val(defaultText);
      }
    });
 
    });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...3(1).lss
File Size:21 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 6 months ago #232736 by holch

I am using limesurvey Version 3.15.8+190130


You might want to think about updating your installation. Almost 3 years without security patches and bug fixes, just saying. ;-)

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Please Log in to join the conversation.

  • Raymond1612
  • Raymond1612's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 6 months ago #232739 by Raymond1612
The script works perfectly. Thanks for that!
And I'll pass on the message about the update to our administrator.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose