Welcome to the LimeSurvey Community Forum

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

need javascript/ expression Manager help

  • ployrich
  • ployrich's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 11 months ago #226942 by ployrich
Please help us help you and fill where relevant:
Your LimeSurvey version:Version 3.27.14
Own server or LimeSurvey hosting: own server
Survey theme/template: bootswatch based
==================
Dear people, javascript and expression manager experts,

With the help of this forum I have built two jquery plugin. (credits go mostly to @tpartner and @jjofm pointing me to the solutions)
One to transform an array with text input to one with partly radioButtons
the other one should accompany it to do soft mandatory: Its working so far as it does stop the skipping and assigns a hidden non response

But what i am stuggeling with is to let the participants pass with the second click on next oder submit. Can somebody help me out pointing me in the right direction. I am suspecting that I need to fire the expressionManager somewhere to "register" the assigned values, but I couldnt figure out where
This is my soft mandatory plugin:
Code:
$.fn.specialArraySF = function(options){
        var settings = $.extend({
        },options);
        return this.each(function() {
        $('#ls-button-submit').on('click', function(e) {
            var failTest = "";
            $(' tr[id^="javatbd"]').each(function(i) {
       
            if ($('td > input[type="text"]', this).val() === "") {
              $(this).addClass('has-error');
                     failTest = 1;
              $('th, td', this).css({'background':'#F39A8B'});
              document.querySelector('#softmandatory-alert').style.visibility = "visible";
 
            } else {
                $(this).removeClass('has-error');
                $('th, td', this).css({'background':'initial'});
            }
        });
          if(failTest==1){           
            setTimeout(assignNonresponse(), 100);
            e.preventDefault();
            e.stopPropagation();
        } else {
            $('.exclusive-item input[type="text"]', thisRow).trigger('keyup');
        }
        });
        function assignNonresponse(){
            $(' tr[id^="javatbd"].has-error').each(function (i) {
                $(this).removeClass("has-error");
                $(".answer_cell_m97", this).each(function(i){
                    $("input[id$='_cbox']", this)[0].checked = true;
                    
                });
              console.log($(".answer_cell_m97", this)[0].value);
              // too early for expressionmanager $('.exclusive-item input[type="text"]', thisRow).trigger('keyup');
 
                });
        }   
    });       
};

Thank you very much!

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 11 months ago #226987 by tpartner
Replied by tpartner on topic need javascript/ expression Manager help
Can you attache a small sample survey (.lss file) containing only the relevant question?

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.

  • ployrich
  • ployrich's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 11 months ago - 1 year 11 months ago #226996 by ployrich
Replied by ployrich on topic need javascript/ expression Manager help
Yes sure I attached a lss file.
Also this is the plugin for transforming the array. Could very well be that the error already lies there.

Code:
$.fn.mixedArrayPlugin = function (options) {
        var settings = $.extend({
            qid : ''
        },options);
        
    return this.each(function(){var qID = settings.qid;
    var thisQuestion = $('#question' + qID);
 
    //this is just for style
    var table = document.querySelector('.mixedArray > .answer-container > table');
 
    table.classList.remove("table-bordered");
    table.classList.add("table");
    
// find input label and hide it
    var ths = $("th");
        for (var t of ths) {
            if (t.textContent.includes("input")) {
                t.style.visibility="hidden";
            }
        }
 
    //setting up the different input styles
    var rows = $('tr.questions-list');
    for (var r of rows) {
        for (i = 0; i < r.children.length; i++) {
            if (i == 1) {
                r.children[i].classList.add('stayText');
            }
            else {
                r.children[i].classList.add('radioB');
                r.children[i].classList.add('exclusive-item');
            }
        }
    }
 
    var change = $('td.radioB > input[type = "text"]');
    change.each(function () {
        var thisID = this.id;
        $(this).hide();
        var radioButton = document.createElement('input');
        radioButton.type = 'radio';
        radioButton.class = 'missingButton';
        radioButton.class = 'radio-item';
        radioButton.id = thisID + '_cbox';
        this.parentNode.appendChild(radioButton);
    });
 
    // removes input on selection of radio, only one radio selectable at once
    $('.exclusive-item input[type="radio"]').on('change', function (e) {
        var thisRow = $(this).closest('tr.subquestion-list');
        var thisCell = $(this).closest('td.answer-item');
        var allB = $('td.radioB', thisRow);
        if ($(this).is(':checked')) {
            $('input[type="text"]', thisCell).val('1');
            $('.stayText input[type="text"]', thisRow).val('');
            for (var a of allB) {
                if (this.parentNode == a) {
                }
                else {
                    //console.log('here', a.children[1]);
                    a.children[1].value = '';
                    a.children[3].checked = false;
                }
            }
        }
        // Fire Expression Manager
        $('.exclusive-item input[type="text"]', thisRow).trigger('keyup');
    });
 
    //remove checked radio on text input
    $('.stayText input[type="text"]').on('keyup change', function (e) {
        var thisRow = $(this).closest('tr.subquestion-list');
        var thisCell = $(this).closest('td.answer-item');
        var allB2 = $('td.radioB', thisRow);
        if ($.trim($(this).val()) !== '') {
            for (var a of allB2) {
                a.children[1].value = '';
                a.children[3].checked = false;
            }
        }
        // Fire Expression Manager
        $('.exclusive-item input[type="text"]', thisRow).trigger('keyup');
       
        
    });
    });
};[/i][/i][/i]

 
Last edit: 1 year 11 months ago by ployrich. Reason: moved comment in code snippet

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 11 months ago #227001 by tpartner
Replied by tpartner on topic need javascript/ expression Manager help
I meant a survey export containing your code.

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.

  • ployrich
  • ployrich's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 11 months ago #227182 by ployrich
Replied by ployrich on topic need javascript/ expression Manager help
sorry i missed your answer. I tried to recounstruct it with the javascript inline.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 11 months ago #227223 by tpartner
Replied by tpartner on topic need javascript/ expression Manager help
A few things...

1) You assigned an invalid ID to the "softmandatory-alert" element. It should not start with a # symbol - www.w3.org/TR/html401/types.html#type-name .
2) As a result, in the JavaScript, your target for this element is incorrect, throwing an error.
3) When the validation fails the first time, you should assign a class to the "softmandatory-alert" element. You can then check for this class on subsequent submit attempts.

Here is a new softMandatory() function:

Code:
  function softMandatory() {
 
    $('#ls-button-submit').on('click', function (e) {
      var failTest = "";
      $(' tr[id^="javatbd"]').each(function (i) {
 
        if ($('td > input[type="text"]', this).val() === "") {
          $(this).addClass('has-error');
          failTest = 1;
          $('th, td', this).css({ 'background': '#F39A8B' });
 
        } else {
          $(this).removeClass('has-error');
          $('th, td', this).css({ 'background': 'initial' });
        }
      });
      if (failTest == 1 &amp;&amp; !$('#softmandatory-alert').hasClass('shown')) {
        console.log("stopping the default");
        setTimeout(assignNonresponse(), 100);
        e.preventDefault();
        e.stopPropagation();
        $('#softmandatory-alert').css('visibility', 'visible').addClass('shown');
      } 
 
    });
    function assignNonresponse() {
      $(' tr[id^="javatbd"].has-error').each(function (i) {
        $(this).removeClass("has-error");
        $(".answer_cell_m97", this).each(function (i) {
          $("input[id$='_cbox']", this)[0].checked = true;
 
        });
        console.log($(".answer_cell_m97", this)[0].value);
      });
 
    }
  }

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.

  • ployrich
  • ployrich's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 11 months ago #227432 by ployrich
Replied by ployrich on topic need javascript/ expression Manager help
Thank you. I think the # is a copy and paste mistake. I will try your suggestion with the additional class for the "alert" I never considered that.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose