View Issue Details

This bug affects 1 person(s).
 4
IDProjectCategoryView StatusLast Update
19998Bug reportsQuestion themepublic2025-03-06 09:41
Reporteralasun Assigned To 
PrioritynoneSeveritycrash 
Status newResolutionopen 
Product Version6.6.x 
Summary19998: After submitting the questionnaire, can go to the smallest unanswered question?
Description

I designed a questionnaire with 6 questions in it, all of which are required. I answered the first 4 questions, but did not answer the last 2 questions, 5 and 6. Click Submit, one or more unanswered questions will pop up on the page, and the page will reach the top. Can the page automatically return to the unanswered question position, which is the smallest serial number, such as 5?

Steps To Reproduce

Steps to reproduce

Add the following code in the theme's cutom.js, and the situation will be better.

Expected result

$(document).on('ready pjax:scriptcomplete', function () {
    // Listen to form submission event
    $('#limesurvey').on('submit', function (e) {
        // Check if there are any unanswered mandatory questions
        var unansweredQuestions = $('.question-container.mandatory').filter(function () {
            return $(this).find('input:checked, textarea').length === 0;
        });

        if (unansweredQuestions.length > 0) {
            // Prevent form submission
            e.preventDefault();

            // Find the first unanswered question
            var firstUnansweredQuestion = unansweredQuestions.first();

            // Create a warning message
            var alertMessage = $('<div class="alert alert-warning alert-dismissible fade show" role="alert">' +
                'One or more mandatory questions have not been answered. Please complete them before continuing.' +
                '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                '</div>');

            // Insert the warning message below the unanswered question
            alertMessage.insertAfter(firstUnansweredQuestion);

            // Scroll to the unanswered question
            $('html, body').animate({
                scrollTop: firstUnansweredQuestion.offset().top - 100
            }, 'fast');

            // Highlight the unanswered question
            firstUnansweredQuestion.addClass('unanswered-highlight');

            // Automatically hide the warning message after 2 seconds
            setTimeout(function () {
                alertMessage.alert('close'); // Use Bootstrap's alert method to close the message
            }, 2000);
        }
    });

    // Listen to user input events (selecting answers or entering text)
    $(document).on('change input', '.question-container.mandatory input, .question-container.mandatory textarea', function () {
        var questionContainer = $(this).closest('.question-container');

        // Remove the highlight if the question has been answered
        if (questionContainer.find('input:checked, textarea').length > 0) {
            questionContainer.removeClass('unanswered-highlight');
        }

        // Check if all mandatory questions have been answered
        var allAnswered = $('.question-container.mandatory').filter(function () {
            return $(this).find('input:checked, textarea').length === 0;
        }).length === 0;

        // Enable the submit button if all mandatory questions have been answered
        if (allAnswered) {
            $('#ls-button-submit').prop('disabled', false);
        }
    });

    // Add styles for unanswered questions
    $('<style>.unanswered-highlight { border: 2px solid red; padding: 10px; }</style>').appendTo('head');
});

Actual result

When you click submit, the page will automatically point to the first unanswered question, and a red box will be drawn around the first unanswered question. When you answer it, the red box will disappear. However, the submit button will be grayed out only if all questions are answered.

TagsNo tags attached.
Attached Files
Bug heat4
Complete LimeSurvey version number (& build)6.10.3
I will donate to the project if issue is resolvedNo
Browser
Database type & versionmysql5.7.30
Server OS (if known)windows11+upupw ank1.1.8
Webserver software & version (if known)Apache/2.4.43
PHP Versionphp 7.4.6

Users monitoring this issue

DenisChenu

Activities

DenisChenu

DenisChenu

2025-03-06 09:41

developer   ~82215

Work in 5.X version

Issue History

Date Modified Username Field Change
2025-03-06 00:30 alasun New Issue
2025-03-06 00:30 alasun File Added: Snipaste_2025-02-26_20-08-51_2025-02-26.png
2025-03-06 00:30 alasun File Added: Snipaste_2025-02-26_20-09-19_2025-02-26.png
2025-03-06 00:30 alasun File Added: Snipaste_2025-02-26_20-09-51_2025-02-26.png
2025-03-06 09:41 DenisChenu Issue Monitored: DenisChenu
2025-03-06 09:41 DenisChenu Bug heat 0 => 2
2025-03-06 09:41 DenisChenu Note Added: 82215
2025-03-06 09:41 DenisChenu Bug heat 2 => 4