Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Mandatory rows in array

Mandatory rows in array 2 years 3 months ago #81556

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I have an array question using radio buttons and I'd like to make all rows mandatory except the last one.
I found this script in the workarounds section but I think this is for an array of text fields. I tried adapting it for radio buttons but had no luck. Can anyone help?
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
 
  // Interrupt the submit function
  $('form#limesurvey').submit(function () {
    // Override the built-in "disable navigation buttons" feature
    $('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
    var empty = 0;
 
    var warningText = 'Please complete the highlighted inputs.';
 
    // Call the mandatory row function with question IDs and row numbers
    mandatoryRow(QQ, 1);
    mandatoryRow(QQ, 3);
 
    // A function to render rows of an array mandatory
 
 
    function mandatoryRow(qID, rowNum) {
 
      $('div#question' + qID + ' table.question tbody[id^="javatbd"]:eq(' + Number(rowNum - 1) + ') input[type="text"]').each(function (i) {
        if ($(this).val() == '') {
          $(this).css('background-color', 'pink');
          empty = 1;
        } else {
          $(this).css('background-color', '#FFFFFF');
        }
      });
    }
 
    if (empty == 1) {
      alert(warningText);
      return false;
    } else {
      return true;
    }
  });
});
</script>
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81569

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
Try this. Replace "QQ" with the question ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		// Call the mandatory row function with question ID
		mandatoryRows(QQ);
 
		// A function to render all but the last rows of an array mandatory
		function mandatoryRows(qID) {
 
			var warningText = 'Please complete the highlighted rows.';
 
			// Add a mandatory class to all rows except the last one
			$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr').addClass('mandatory');
 
			// Interrupt the submit function
			$('#movenextbtn, #movesubmitbtn').click(function(){
				var fail = '';
 
				// Loop through all mandatory rows and check for answers
				$('tr.mandatory').each(function(i){
					if($('input.radio:checked', this).length < 1) {
						 $('th, td', this).css({ 'background':'pink' });
						 fail = 1;
					}
					else {
						 $('th, td', this).css({ 'background':'' });
					}
				});
 
				if(fail == 1) {
					alert (warningText);
					return false;
				}
				else {
					return true;
				}
			});
		}
	});
 
</script>
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 2 years 3 months ago by tpartner.
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81572

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi Tony

Hope you are well!

Thanks for the script, which works fine. I had actually just muddled something together which seemed to work but wasn't as neat as your solution.
I have one small thing to add in that there is a short textfield just after my array question and I need to check if somebody selected an answer on the last row of my array but didn't fill in the textbox question I need to warn them.

I think I have most of the script setup to do this but how do I assign the value in the textbox to a variable?
I can't use INSERTANS because it's before the submit button is pressed.

Cheers

Paul
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81574

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
If it's a single-short-text (where QQ is the question ID):
var textInput = $('#questionQQ input.text').val();
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81575

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Great, thanks Tony....I knew it was something simple!
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81583

  • Bourbon
  • Bourbon's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
How do you get them all to be mandatory?
I can't get the script in workaround section to work, but this did.
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81584

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
To get them all to be mandatory, you don't need javascript at all....just set the option at the bottom of the page when you edit the question.
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81590

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
If you want to make them all mandatory and use this custom behaviour (turning the offending rows pink), change this:
$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr').addClass('mandatory');

To this:
$('div#question'+qID+' table.question tbody[id^="javatbd"] tr').addClass('mandatory');
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81756

  • Bourbon
  • Bourbon's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
To get them all to be mandatory, you don't need javascript at all....just set the option at the bottom of the page when you edit the question.
Sorry I was a bit vague. I ment that all rows are mandatory and the rows turn pink in unasnwered.


One problem occurs now.
"Repeating headings in array questions" is now turned pink, and I cant get past the array question, because the repeating heading row can't be answered.
Last Edit: 2 years 3 months ago by Bourbon. Reason: problem found
The administrator has disabled public write access.

Re: Mandatory rows in array 2 years 3 months ago #81765

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
Please supply a sample question or survey so I can test.
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.131 seconds
Donation Image