Check out the LimeSurvey source code on GitHub!

(SOLVED) JS warning even if question is not visible

More
5 years 3 months ago - 5 years 3 months ago #68583 by waitz
I have a js in a question that sets it as required and shows an error if nothing in a dropdown is selected:
Nationality *<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		var tokenVal = '{TOKEN:ATTRIBUTE_49}';
		var qID = 888;
 
		$('#question'+qID+' option').filter(function() {
			return $(this).text() == tokenVal;
		}).attr('selected', 'selected');
 
 
 
	});
</script><script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		dropdownRequired(888);
 
		function dropdownRequired(qID) {
 
			var warningText = 'Please select your nationality.';
 
			// Interrupt the submit function
			$('#movenextbtn').click(function () {
				// Override the built-in "disable navigation buttons" feature
				$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
				if ($('#question'+ qID+ ' option:selected').attr('value') == '') {
					$('#question'+ qID+ ' select').css({'background':'pink'});
					alert(warningText);
					return false;
				}
				else {
					$('#question'+ qID+ ' select').css({'background':''});
					return true;
				}
			});
		}
	});
</script>
The problem is that a js error is popping up even if the <a href=' docs.limesurvey.org/tiki-index.php?page=Setting%20conditions '>conditions</a> of the question is hiding the question. Isn't that strange?

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 5 years 3 months ago by waitz.

Please Log in to join the conversation.

More
5 years 3 months ago #68589 by tpartner
That is normal behaviour - your script has no way of knowing if the question is hidden

Replace this line:
if ($('#question'+ qID+ ' option:selected').attr('value') == '') {
With this:
if ($('#question'+ qID).is(":visible") && $('#question'+ qID+ ' option:selected').attr('value') == '') {

Cheers,
Tony Partner

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

Please Log in to join the conversation.

More
5 years 3 months ago #68591 by waitz
Ahh, lovely. Of course it couldn't know that ;)

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form