Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:

THEMA: Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 1 Jahr 3 Monate her #99048

  • mktfan
  • mktfans Avatar
Hi,

I need your help with a javascript code.

I have an array question with checkboxes. I am trying to exclude all other options in a row if the user the last one. I've this article where you can find a javascript code to do it:

manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others

The code proposed is:
<script type="text/javascript" charset="utf-8">
 
        $(document).ready(function() {
 
                // Call the exclude function using question ID(s)
                excludeOpt (QQ);
 
                // A function to make the last option in each array row exclusive
                function excludeOpt (qID) {
 
                        // Add some classes to the checkboxes so we can manipulate them
                        $('#question'+qID+' table.question tbody td').addClass('normalOpt');
                        $('#question'+qID+' table.question tbody').each(function(i) {
                                $('td:last', this).removeClass('normalOpt').addClass('exlusiveOpt')
                        });
 
                        // A listener on the checkbox cells
                        $('#question'+qID+' table.question tbody td').click(function (event) {
 
                                // Set some vars
                                var el = $(this).parent();
                                var optLength = $('td', el).length
 
                                // Uncheck the appropriate boxes in a row
                                if ($(this).hasClass('normalOpt')) {
                                        $('td:last input[type=checkbox]', el).attr('checked', false);
                                }
                                else {
                                        $('td', el).each(function(i) {
                                                if (i < (optLength - 1)) {
                                                        $('input[type=checkbox]', this).attr('checked', false);
                                                }
                                        });
                                }
                        });
 
                        // A listener on the checkboxes
                        $('#question'+qID+' table.question tbody td input[type=checkbox]').click(function (event) {
 
                                // Set some vars
                                var el2 = $(this).parent().parent();
                                var optLength = $('td', el2).length
 
                                // Uncheck the appropriate boxes in a row
                                if ($(this).parent().hasClass('normalOpt')) {
                                        $('td:last input[type=checkbox]', el2).attr('checked', false);
                                }
                                else {
                                        $('td', el2).each(function(i) {
                                                if (i < (optLength - 1)) {
                                                        $('input[type=checkbox]', this).attr('checked', false);
                                                }
                                        });
                                }
                        });
                }
        });
</script>

I've used it, but something weird happens. Please, have a look to my survey here:

enquestas.com/sv/index.php/333559/lang-es

Code only works in the last row, but not in the 2 first one.

I am using Limesurvey V2.0

Can you help me, please?

Many thanks in advance

Regards

Víctor
Letzte Änderung: 1 Jahr 3 Monate her von mktfan. Begründung: faltaba URL
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 1 Jahr 2 Monate her #99988

  • w0928
  • w0928s Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 33
  • Dank erhalten: 3
  • Karma: 1
I'm having the same problem. Can someone please post the code that actually works? I've tried manipulating the code myself but I am having no luck whatsoever.
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 1 Jahr 2 Monate her #100005

  • tpartner
  • tpartners Avatar
  • ONLINE
  • LimeSurvey Team
  • Beiträge: 4364
  • Dank erhalten: 827
  • Karma: 381
Try this. Note that if using 1.92+, you do not need to replace "{QID}" - this tag will automatically insert the question ID:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// Call the exclude function using question ID
		excludeOpt ({QID});
	});
 
	// A function to make the last option in each array row exclusive
	function excludeOpt (qID) {
 
		var thisQuestion = $('#question'+qID)
 
		// Add some classes to the checkbox cells
		$('table.question tbody td', thisQuestion).addClass('normalOpt');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt')
		});
 
		// A listener on the checkbox cells
		$('table.question tbody td', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
 
			// Uncheck the appropriate boxes in a row
			if ($(this).hasClass('normalOpt')) {
				$('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false);
			}
			else {
				$('.normalOpt input[type=checkbox]', thisRow).attr('checked', false);
			}
		});
 
		// A listener on the checkboxes
		$('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
			var thisCell = $(this).closest('td');
 
			// Uncheck the appropriate boxes in a row
			if ($(thisCell).hasClass('normalOpt')) {
				$('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false);
			}
			else {
				$('.normalOpt input[type=checkbox]', thisRow).attr('checked', false);
			}
		});
	}	
</script>


I have updated the workaround - manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others
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.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Folgende Benutzer bedankten sich: w0928

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 6 Tage 11 Stunden her #114688

  • MVT
  • MVTs Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 36
  • Karma: 0
Hi,

I tried this but...
On some lines, the last column is "unchecked" after one or more other check-boxes checked.

I've a check-box matrix where the responses are mandatory.
I tried your snippet alone and with one other (check one column by default to avoid validation problems, I mean I've add a column on the right named "N/A" (NSP in French), and this colomn is checked by default on load.

It should be easier with the test survey.

Dateianhang:

Dateiname: Test_Checkbox_619228.lss
Dateigröße: 27 KB


Limesurvey Release : 205plus-build141113

May be I did something wrong ?

Thanks for your help

<EDIT>
because of my insufficient knowledge in javascript, I suppose I did somme errors.
I've corrected the script in order to make it more "correct" but it doesn't work well
 < script type = "text/javascript" charset = "utf-8" >
	$(document).ready(function () {
 
		// Call the function with a question ID and column number Once
		var wQuit = true;
 
		if (wQuit) {
			checkedDefault({
				QID
			}, 12);
			wQuit = false;
		};
 
		// Call the exclude function using question ID
 
		excludeOpt({
			QID
		});
 
	});
 
// A function to pre-check a column of an array
 
function checkedDefault(qID, column) {
	var checkedCol = column - 1;
	$('#question' + qID + ' tr.subquestions-list').each(function (i) {
		if ($('input.checkbox:checked', this).length == 0) {
			$('input.checkbox:eq(' + checkedCol + ')', this).prop('checked', true);
			$('input.checkbox:eq(' + checkedCol + ')', this).parent().find('input[type="hidden"]').val(1);
		}
	});
}
// A function to make the last option in each array row exclusive
 
function excludeOpt(qID) {
 
	var thisQuestion = $('#question' + qID)
 
		// Add some classes to the checkbox cells
		$('table.subquestions-list tbody td', thisQuestion).addClass('normalOpt');
	$('table.subquestions-list tbody tr', thisQuestion).each(function (i) {
		$('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt')
	});
 
	// A listener on the checkbox cells
 
	$('table.subquestions-list tbody td', thisQuestion).click(function (event) {
 
		// Set some vars
		var thisRow = $(this).closest('tr');
 
		// Uncheck the appropriate boxes in a row
		if ($(this).hasClass('normalOpt')) {
			$('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false);
		} else {
			$('.normalOpt input[type=checkbox]', thisRow).attr('checked', false);
		}
	});
 
	// A listener on the checkboxes
 
	$('table.subquestions tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
		// Set some vars
		var thisRow = $(this).closest('tr');
		var thisCell = $(this).closest('td');
 
		// Uncheck the appropriate boxes in a row
		if ($(thisCell).hasClass('normalOpt')) {
			$('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false);
		} else {
			$('.normalOpt input[type=checkbox]', thisRow).attr('checked', false);
		}
	});
}
 
<  / script >
Letzte Änderung: 6 Tage 10 Stunden her von MVT. Begründung: modifications
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 5 Tage 22 Stunden her #114732

  • tpartner
  • tpartners Avatar
  • ONLINE
  • LimeSurvey Team
  • Beiträge: 4364
  • Dank erhalten: 827
  • Karma: 381
Okay, so you want the last column to be checked by default and the last column is also exclusive.

Here's a simplified script to do that:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		//// Check the last option in each row by default ////
		$('tr.subquestions-list', thisQuestion).each(function(i) {
			if ($('input.checkbox:checked', this).length == 0) {
				$('input.checkbox:last', this).prop('checked', true);
				$('input.checkbox:last', this).parent().find('input[type="hidden"]').val(1);
			}
		});
 
		//// Make the last option in each array row exclusive ////
		// Add some classes to the checkbox cells
		$('table.subquestions-list tbody td', thisQuestion).addClass('normalOpt');
		$('table.subquestions-list tbody tr', thisQuestion).each(function(i) {
			$('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt')
		});
		// A listener on the checkboxes
		$('input[type=checkbox]', thisQuestion).change(function (event) {
 
			var thisRow = $(this).closest('tr');
			var thisCell = $(this).closest('td');
 
			if ($(this).is(':checked') && thisCell.hasClass('normalOpt')) {
				$('.exlusiveOpt input[type=checkbox]', thisRow).prop('checked', false);
				$('.exlusiveOpt input[type="hidden"]', thisRow).val('');
			}
			if ($(this).is(':checked') && thisCell.hasClass('exlusiveOpt')) {
				$('.normalOpt input[type=checkbox]', thisRow).prop('checked', false);
				$('.normalOpt input[type="hidden"]', thisRow).val('');
			}
		});
 
    });
</script>

And a working 2.05 survey:

Dateianhang:

Dateiname: limesurvey_survey_619228_TP.lss
Dateigröße: 27 KB
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.
Letzte Änderung: 5 Tage 22 Stunden her von tpartner.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Folgende Benutzer bedankten sich: MVT

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 5 Tage 12 Stunden her #114743

  • MVT
  • MVTs Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 36
  • Karma: 0
Ok, thanks.

I'll try it on my new survey.
I've to redesign it in order to simplify the job for my respondents... if I want to have answers !

The best way should be to chain surveys in a conditional mode, I mean, to show the first part (participation after inscription), save some data and, at the end of the first survey, send an invitation for the second part (survey) with some data completed. But it is for "the future".

This first snippet should simplify the job for my first survey.

Regards

Michel
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Moderatoren: ITEd
Ladezeit der Seite: 0.227 Sekunden
Donation Image