Welcome, Guest
Username: Password: Remember me

TOPIC: (SOLVED) JS warning even if question is not visible

(SOLVED) JS warning even if question is not visible 2 years 9 months ago #68583

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
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: 2 years 9 months ago by waitz.
The administrator has disabled public write access.

Re: JS warning even if question is not visible 2 years 9 months ago #68589

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4130
  • Thank you received: 763
  • Karma: 347
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

(SOLVED) Re: JS warning even if question is not visible 2 years 9 months ago #68591

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Ahh, lovely. Of course it couldn't know that ;)
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.169 seconds
Donation Image