check which answer (radio list) was checked and alert if answer code is wrong

More
3 weeks 4 days ago #173104 by elihaz
elihaz created the topic: check which answer (radio list) was checked and alert if answer code is wrong
Hi

I need that every time a user check a radio button from the list, the answer value (or even better answer code e.g A1)
will be copmared to the right answer, and alerted if wrong. tried this not working jquery:
 
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
 		var thisQuestion = $('#question{QID}');
        var q1Ans = '';
			if($('input.radio:checked', thisQuestion).length > 0) {
			   q1Ans = $('input.radio:checked', thisQuestion).attr('value');
			   if(q1Ans != "let's be friend") {
					alert("wrong!");
			}
 
 
		});
	});
</script>
 

Any Ideas? Tnx.

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173119 by tpartner
tpartner replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
1) You will need to test the answer codes, not the answer text.

2) I see no listener on the radio buttons.

What Limesurvey version are you using?

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

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173128 by DenisChenu
DenisChenu replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
And why not, in help for example :
{if(self.NAOK=="A1","","Error")}
Or use manual.limesurvey.org/Question_type_-_Li...28em_validation_q.29

Else : $('input.radio:checked', thisQuestion).attr('value') give the code, not the text …

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173135 by tpartner
tpartner replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
I wonder, why present a list-radio question with multiple answers if you won't allow wrong answers.

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: DenisChenu

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173136 by elihaz
elihaz replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
2.5+ build 16037

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173140 by elihaz
elihaz replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
Hi
Thank you for helping.
1) yep... forgot the listener.
2) debugging> I think it's the "var thisQuestion = $('#question{QID}');"
the following code is working but the alert shows [object object]
 
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		$('#question{QID} input.radio').on('click', function(e){
	 		var thisQuestion = $('#question{QID}');
			alert(thisQuestion);  // [object Object]
 
 
		});
	});
</script>

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173144 by DenisChenu
DenisChenu replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
Did you read ?

Did you try to understand jquery ?

Else : $('input.radio:checked', thisQuestion).attr('value') give the code, not the text …


Then : if(q1Ans != "A1") {

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in or Create an account to join the conversation.

More
3 weeks 4 days ago #173146 by tpartner
tpartner replied the topic: check which answer (radio list) was checked and alert if answer code is wrong
Try this (untested):

<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		var correctCode = 'A1':
 
		$('#question{QID} input[type="radio"]').on('click', function(e){
	 		var thisVal = $(this).val();
 
			if(thisVal != correctCode) {
				alert('Wrong answer dummy!');  
			}
		});
	});
</script>

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

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!