Conditions not working

More
3 years 10 months ago - 3 years 10 months ago #100167 by w0928
w0928 created the topic: Conditions not working
I have an array(numbers) checkbox style question with 4 columns. The first 2 columns are mutually exclusive from all the others... so the user can only check column 1 by itself, column 2 by itself, column 3 with or without 4, and column 4 with or without 3.

I have the code below that implements this behavior.

In addition to this question I have another question with a condition on it that says to only show that question when the user has checked columns 2, 3, or 4 for one of the rows in my array(numbers) checkbox style question. I know the condition is set properly b/c if I remove the code below it works.

If a user checks a checkbox in columns 2, 3, or 4 then condition works and the question is shown. However, if the user checks on column 1, which triggers the code to uncheck columns 2, 3, and 4... the subsequent question still appears. It doesn't go away like it should.

So basically I am asking why the code below is causing this and how can I go about fixing it? I'm guessing I need to call .trigger('click') to uncheck those columns, but then I get stuck in a loop. Sorry if this is more of a javascript question. I'm just trying to figure out how this all works and would really, really appreciate any help you all could provide.

Thanks.
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// Call the exclude function using question ID
		excludeOpt ({QID});
	});
 
	// A function to make the first 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('nonExclusiveOpt1');
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
			$('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
		});
 
		// 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('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}				
			else if ($(this).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(this).hasClass('nonExclusiveOpt1') && $(this).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 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('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('nonExclusiveOpt1') && $(thisCell).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
		});
	}	
</script>
Last Edit: 3 years 10 months ago by w0928.

Please Log in to join the conversation.

More
3 years 10 months ago #100190 by tpartner
tpartner replied the topic: Conditions not working
Ah, we'll have to fire the fixnum_checkconditions() function for all affected checkboxes.

Try this:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// Call the exclude function using question ID
		excludeOpt ({QID});
	});
 
	// A function to make the first 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('nonExclusiveOpt1');
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
			$('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
		});
 
		// 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('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}				
			else if ($(this).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(this).hasClass('nonExclusiveOpt1') && $(this).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
 
			checkRowConditions(thisRow);
		});
 
		// 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('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('nonExclusiveOpt1') && $(thisCell).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
 
			checkRowConditions(thisRow);
		});
 
		// A function to check conditions for all checkboxes in a row
		function checkRowConditions(thisRow) {
			$('input[type="checkbox"]', thisRow).each(function(i) {
				var thisAnsID = $(this).attr('id').replace(/cbox_/, '');
				var thisAnswerInput = document.getElementById('answer'+thisAnsID);
				var thisJavaInput = document.getElementById('java'+thisAnsID);
				var thisAnsValue = 0;
				if(this.checked) {
					thisAnsValue = 1; 
				} 
				thisAnswerInput.value = thisAnsValue;
				thisJavaInput.value = thisAnsValue;
				fixnum_checkconditions(thisAnsValue, thisAnsID, thisAnswerInput.type);  
				return true;
			});
		}
	}	
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: w0928

Please Log in to join the conversation.

More
3 years 10 months ago #100192 by w0928
w0928 replied the topic: Conditions not working
Thank you! I would have never figured this out on my own.

Please Log in to join the conversation.

Did you already participate in our customer survey?

Don't miss your chance for great prices.

Please click here to participate:

Start now

Start now!

Just create your account and start using Limesurvey today.

Register now