Welcome to the LimeSurvey Community Forum

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

Array(Number) X-Scale Last Option Other Spcify

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 week 2 days ago #270152 by rajkumar_dms
Please help us help you and fill where relevant:
LimeSurvey version: [6.13]
Own server or LimeSurvey Cloud:Cloud
Survey theme/template:Twenty Three
==================

I had made a Array(Number) Question with Check box layout but i need X-scale last option would have other specify text input

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 week 2 days ago #270154 by rajkumar_dms
Replied by rajkumar_dms on topic Array(Number) X-Scale Last Option Other Spcify
I had use this script but it's not working as per my need
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
addOtherTextbox({QID});
});

function addOtherTextbox(qID) {
var thisQuestion = $('#question' + qID);

// Loop through each row in the question
$('tr.subquestion-list', thisQuestion).each(function() {
var lastCell = $(this).find('td.checkbox-item:last');
var checkbox = lastCell.find('input[type="checkbox"]');
var label = lastCell.find('label');

// Prevent duplicate text boxes
if (!lastCell.find('input.other-text').length) {
// Add inline text box next to the checkbox
$('<input type="text" class="other-text" placeholder="Please specify" ' +
'style="margin-left: 10px; width: 300px; display: inline-block; font-size: 1em; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">'
).insertAfter(label);
}

// Automatically check the checkbox when typing in the text box
lastCell.find('input.other-text').on('input', function() {
if ($(this).val().trim() !== '') {
checkbox.prop('checked', true);
} else {
checkbox.prop('checked', false);
}
});

// Clear the text box if the checkbox is unchecked
checkbox.on('change', function() {
if (!checkbox.is(':checked')) {
lastCell.find('input.other-text').val('');
}
});
});

// Add form validation to prevent submission if checkbox is checked and text box is empty
$('#limesurvey input[type="submit"]').on('click', function(event) {
var hasError = false;

// Loop through each row and check the conditions
$('tr.subquestion-list', thisQuestion).each(function() {
var lastCell = $(this).find('td.checkbox-item:last');
var checkbox = lastCell.find('input[type="checkbox"]');
var textbox = lastCell.find('input.other-text');

// If checkbox is checked, check if text box is filled
if (checkbox.is(':checked') && textbox.val().trim() === '') {
textbox.css('border', '2px solid red'); // Add red border for invalid input
hasError = true;
} else {
textbox.css('border', '1px solid #ccc'); // Reset border if valid
}
});

// If there's an error, prevent form submission and alert the user
if (hasError) {
alert('Please fill in the text box when selecting "Other (please specify)".');
event.preventDefault(); // Prevent form submission
}
});
}
</script>


When i check the last option and no input for other text box not show any error

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 2 days ago #270155 by Joffm
Where did you find this script?
It seems to be for a verfy old version.
So it's not surprising.

And what do you want to achieve?
Something like this?
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 week 2 days ago #270156 by rajkumar_dms
Replied by rajkumar_dms on topic Array(Number) X-Scale Last Option Other Spcify
Yes

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 1 day ago - 1 week 1 day ago #270157 by Joffm
Hi,
again I ask: "Where did you find this script?"
You see the script only adds a text input visually.
Code:
// Add inline text box next to the checkbox
$('<input type="text" class="other-text" placeholder="Please specify" ' +
'style="margin-left: 10px; width: 300px; display: inline-block; font-size: 1em; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">'
).insertAfter(label);

But where is the field in the database to store the content? There is no.
Did you check this by activating the survey and investigating the response table?

If you want to insert additional elements you have to create them in LimeSurvey.
This is usually done by inserting the text input fields of a following "multiple short text" question into the array by a script.

The example I showed works a bit different.
There is no text field added to the checkboxes of an array(number), but it is based on an array(text) and some text fields are visually converted to checkboxes.

And I am quite sure that in the entire application there is no ID called #limesurvey.
The submit button you address like this: $('#ls-button-submit')
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 week 1 day ago by Joffm.

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 week 1 day ago #270158 by rajkumar_dms
Replied by rajkumar_dms on topic Array(Number) X-Scale Last Option Other Spcify
Thanks for this

i made some R&D with lime survey forum and Chatgpt i get this

I had check the same the content not store entered data

Can you help this by using "multiple short text"

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 1 day ago #270161 by Joffm
Here two scripts.

1. Create a "multiple short text" question after your array(numbers) question
Code:
<script type="text/javascript" charset="utf-8">
     $(document).on('ready pjax:scriptcomplete',function(){
         // Identify the questions
        var qArrayID = {QID};
        var qArray = $('#question'+qArrayID);
        var qMultiText = qArray.nextAll('.multiple-short-txt:eq(0)');
 
              // Hide the multi-short-text question
        $(qMultiText).hide();
 
        var tableWidth = $('table.questions-list:eq(0)', qArray).width();
        var answerWidth = $('col.ls-col-odd:eq(0)', qArray).width();
        var answerWidthPercent = (answerWidth/tableWidth)*100;
        var answersLength = $('col.ls-col-odd, col.ls-col-even', qArray).length;
        var answerWidthPercent2 = (answerWidthPercent*answersLength)/(answersLength+1)
        $('table.questions-list col', qArray).removeAttr('width');
        $('table.questions-list col:not(.col-answers)', qArray).css('width', 'auto');
        $('table.questions-list thead tr', qArray).append( '<th class="answer-text inserted-column-label" /></th>');
        $('table.questions-list thead tr th.answer-text.inserted-column-label', qArray).css('width','30%');
        $('tr.subquestion-list', qArray).each(function(i) {
            $(this).append('<td class="answer-item text-item">\
            </td>\
            ');
        });
        // Load the column label for the text inputs
        $('.inserted-column-label:eq(0)', qArray).text($('.ls-label-question', qMultiText).text());
        $('.text-item', qArray).append('<label class="ls-label-xs-visibility">'+$('.ls-label-question', qMultiText).text()+'</label>');
        // Loop through the multi-short-text sub-questions
        $('li.answer-item', qMultiText).each(function(i) {
            // Move the text inputs into the array
            $('input[type="text"]', this).appendTo($('tr.subquestion-list:eq('+i+') .text-item', qArray));
        });
    });
</script>
Question text of text question is the label of the "Other" option.

2. With an array(text)
Code:
<script type="text/javascript" charset="utf-8">
    
    
    $(document).on('ready pjax:scriptcomplete',function(){
 
        // Identify this question
        var thisQuestion = $('#question{QID}');
 
        // Column-specific classes
        $('tr.subquestion-list', thisQuestion).each(function(i) {
            $('th, td', this).each(function(i) {
                $(this).addClass('column-'+i);
            });
        });
        
        // Insert checkboxes
        $('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3', thisQuestion).addClass('custom-checkbox-item');
        $('.custom-checkbox-item', thisQuestion).each(function(i) {
            var thisID = $('input:text:eq(0)', this).attr('id');
            $('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />');
            if($('input:text:eq(0)', this).val() == 'Y') {
                $('input:checkbox:eq(0)', this).prop('checked', true);
            }
            $(this).removeClass('text-item').addClass('checkbox-item');
            $('input:text:eq(0)', this).remove();
        });      
    });
</script>


By the way:
Why do you ask all your questions in catagory "Installation and Update" though they are not related to this?
In my opinion you should ask in "Can I do this with LimeSurvey?"

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: rajkumar_dms

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 week 1 day ago #270162 by rajkumar_dms
Replied by rajkumar_dms on topic Array(Number) X-Scale Last Option Other Spcify
thanks for this

I will take care for this form next time

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 days 20 hours ago #270214 by tpartner
Replied by tpartner on topic Array(Number) X-Scale Last Option Other Spcify

And I am quite sure that in the entire application there is no ID called #limesurvey.
The submit button you address like this: $('#ls-button-submit'. 
#limesurvey is the ID of the form. Calling "submit" on it triggers a similar action as calling "click" on #ls-button-submit.

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.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose