Welcome, Guest
Username: Password: Remember me

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

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 11 months 2 weeks ago #99048

  • mktfan
  • mktfan's 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
Last Edit: 11 months 2 weeks ago by mktfan. Reason: faltaba URL
The administrator has disabled public write access.

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 10 months 1 week ago #99988

  • w0928
  • w0928's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 27
  • Thank you received: 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.
The administrator has disabled public write access.

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others 10 months 1 week ago #100005

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4069
  • Thank you received: 747
  • Karma: 341
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.
The administrator has disabled public write access.
The following user(s) said Thank You: w0928
Moderators: ITEd
Time to create page: 0.129 seconds
Donation Image