Make "Submit" button appear only if people answer "Yes" for a Yes/No question

More
3 years 2 months ago #182948 by krosser

DenisChenu wrote: There are no submit button in preview, then the script broke when you try to update an attribute

Add this

if(!$('#ls-button-submit').length != 1) {
return;
}
just before // Identify this question

I don't understand why Expression Manager broke if there are error here … Maybe limesurvey core can do somthing here : EM javascript work with (some) broken JS in question text.



Perhaps I didn't add it in the right place, but it brakes the script.
<script type="text/javascript" charset="utf-8">		
	$(document).ready(function() {
 
if(!$('#ls-button-submit').length != 1) {
return;
}
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Initially disable the Next/Submit button
		$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
 
		// Listeners on Yes/No radios to toggle the Next/Submit button
		$('input:radio[value="A1"]', thisQuestion).click(function(){
			$('#ls-button-submit').prop('disabled', '').removeClass('ui-state-disabled');
		});
		$('input:radio[value="A2"]', thisQuestion).click(function(){
			$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
		});
    });
</script>

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
More
3 years 2 months ago #182949 by tpartner
Try wrapping the whole thing in an if() statement that looks for the submit button:

<script type="text/javascript" charset="utf-8">		
	$(document).ready(function() {
 
		if($('#ls-button-submit').length > 0) {
			// Identify this question
			var thisQuestion = $('#question{QID}');
 
			// Initially disable the Next/Submit button
			$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
 
			// Listeners on Yes/No radios to toggle the Next/Submit button
			$('input:radio[value="A1"]', thisQuestion).click(function(){
				$('#ls-button-submit').prop('disabled', '').removeClass('ui-state-disabled');
			});
			$('input:radio[value="A2"]', thisQuestion).click(function(){
				$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
			});
		}
    });
</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: krosser
The topic has been locked.
More
3 years 2 months ago #182950 by DenisChenu

krosser wrote: Perhaps I didn't add it in the right place, but it brakes the script.

Perhaps i made an error …

It's not ! it with …
<script type="text/javascript" charset="utf-8">		
	$(document).ready(function() {
 
            if($('#ls-button-submit').length != 1) {
                return;
            }
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Initially disable the Next/Submit button
		$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
 
		// Listeners on Yes/No radios to toggle the Next/Submit button
		$('input:radio[value="A1"]', thisQuestion).click(function(){
			$('#ls-button-submit').prop('disabled', '').removeClass('ui-state-disabled');
		});
		$('input:radio[value="A2"]', thisQuestion).click(function(){
			$('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
		});
    });
</script>

Personnaly i prefer to quit when not needed than wrap inside a big if when needed. I think it's more clear

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: krosser
The topic has been locked.
More
3 years 2 months ago #182956 by tpartner

Personnaly i prefer to quit when not needed than wrap inside a big if when needed. I think it's more clear

Yeah but I think that's a matter of personal preference. :)

There is very little difference in execution resources between the two methods, although mine should be marginally quicker as the code inside the IF() statement is not executed.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Start now!

Just create your account and start using Limesurvey today.

Register now