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

TOPIC: Create unique options in array question

Create unique options in array question 4 years 11 months ago #57604

  • paulfiner
  • paulfiner's Avatar
  • Offline
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I think it's from the workaround script but maybe a combination of the two?
I tried increasing the delay in the Timeout function used in the workaround but it didn't work.

Unfortunately my client wants this survey to go live this afternoon and isn't too happy that this doesn't work. It seems like it should be such a straightforward type of question!
The administrator has disabled public write access.

Create unique options in array question 4 years 11 months ago #57605

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5403
  • Thank you received: 1151
  • Karma: 479
That workaround is for mandatory rows in a text array. Did you modify it?
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.

Create unique options in array question 4 years 11 months ago #57606

  • paulfiner
  • paulfiner's Avatar
  • Offline
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Here's the code I am using:
<script type="text/javascript" charset="utf-8">
 
// Wait until the document is fully loaded
$(document).ready(function() { 
 
    /********************** SETTINGS **********************/
 
    // The text to appear in the warning element 
    var warningText = 'Please answer 3 to continue';
 
    // The question ID 
    var questionId = 2326;
 
    // The minimum number of answers required 
    var minNumber = 3;
 
    /******************************************************/
 
    // Create the warning element and place it after the submit button          
    var el = document.createElement('div'); 
 
    el.setAttribute('id','warning');
 
    document.body.appendChild(el);
 
    $( '#warning' ).css({ 
        'border':'1px solid #333333', 
        'width':'200px', 
        'padding':'3px', 
        'color':'red' 
    });
 
    $('#warning').html(warningText);
 
    $('input[type="submit"]').after($('#warning')); 
 
    // Detect the initial number of checked answers & display Next/Submit button accordingly 
    var inputInitCount = 0; 
 
    $('#question' + questionId + ' input').each(function(i) { 
        if ($( this ).attr('checked') == true ) { 
            inputInitCount++; 
        } 
    }); 
 
    if (inputInitCount > (minNumber - 1)) { 
        $('input[type="submit"]').show(); 
        $('#warning').hide(); 
    } 
    else { 
        $('#warning').show(); 
        $('input[type="submit"]').hide(); 
    } 
 
    // Listener to detect number of checked answers & display Next/Submit button accordingly 
    $('#question' + questionId + ' input').click(function() { 
 
        var inputCount = 0; 
 
        $('#question' + questionId + ' input').each(function(i) { 
            if ($( this ).attr('checked') == true ) { 
                inputCount++;
 
            } 
        });
 
 
 
        if (inputCount > (minNumber - 1)) { 
            $('input[type="submit"]').show(); 
            $('#warning').hide(); 
        } 
        else { 
            $('#warning').show(); 
            $('input[type="submit"]').hide(); 
        }  
 
        // The original functions of the click event 
        checkconditions(this.value, this.name, this.type);
    });                    
});
</script>

I've checked the inputCount variable and it gets incremented but it also gets incremented if a user changes their option in a particular column. The crux of the problem is I just need to count the total number of boxes ticked and proceed if this is 3.
The administrator has disabled public write access.

Create unique options in array question 4 years 11 months ago #57607

  • paulfiner
  • paulfiner's Avatar
  • Offline
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I think this workaround might be a bit overkill. I'm going to try a loop and just count the checkboxes before the Next button is pressed.
The administrator has disabled public write access.

Create unique options in array question 4 years 11 months ago #57609

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5403
  • Thank you received: 1151
  • Karma: 479
Well, that's not the workaround you linked below and it will definitely get screwed up by the timer function in this topic's script and the fact that table cells can be clicked.

I think you just need to interrupt the next/submit function. do a column count and proceed accordingly.

Gimme a minute...
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.

Create unique options in array question 4 years 11 months ago #57612

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5403
  • Thank you received: 1151
  • Karma: 479
Try this as the complete script:
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
 
		var qID = 2326;
 
		$('#question'+qID+' table.question tbody td').click(function () { 
		    var cellClass = $(this).attr('class');
			var parent = $(this).parent();
			setTimeout(function() { 
				if($('.'+cellClass+' input[type=radio]:checked').length > 1) {
					$('.'+cellClass+' input[type=radio]').attr('checked', false);
					$('.'+cellClass+' input[type=radio]', parent).attr('checked', true); 
				}
			}, 250);
		});
 
		// Interrupt the submit function and check each column for a checked radio
		$('form#limesurvey').submit(function(){
 
			var warningText = 'Please answer 3 to continue';
			var ok = 1;
 
			$('#question'+qID+' table.question tbody:first td').each(function(i) { 
				var colClass = $(this).attr('class');
				if($('.'+colClass+' input.radio:checked').length < 1) {
					ok = 0;
				}
			});
			if(ok == 0) {
				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.
The administrator has disabled public write access.

Create unique options in array question 4 years 11 months ago #57616

  • paulfiner
  • paulfiner's Avatar
  • Offline
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Many thanks for all your help with this Tony.
I now have it working 100% although not using your last bit of code.

I just have a simple loop that goes through all the radiobuttons and keeps a running total of the checked ones. If this equals 3 then I proceed otherwise I show an alert.

Cheers

Paul
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.261 seconds