Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Mutually Exclusive Answers to a multiple choice question using javascript

  • w0928
  • w0928's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 5 months ago - 10 years 5 months ago #100066 by w0928
This one is probably pretty easy for most folks here but I'm not very well-versed in javascript for LimeSurvey so I have no idea how it is done.

I have a multiple choice question and I would like to make it so that only 1 answer can be checked.

I know what you are thinking: use the exclusive answer option or use a radio-button style question.

The reason I do not want to use a radio button style question is because I want the user to be able to select "no answer" (nothing checked) and it seems as though radio buttons lock you in once a selection is made. I could add a "no answer" answer but I do not want to do this.

The reason I do not want to use the exclusive answer option is because you must uncheck your selection to re-enable the others before you can change your answer. I do not like this behavior. I'd like to allow the user to check on anything at any time and automatically have the other choices unchecked without having to uncheck the current selection first.

Appreciate any assistance. Thank you.

EDIT: Also, if anyone knows how to do this with an array(numbers)(checkbox style) question that would also be super helpful. :)
Last edit: 10 years 5 months ago by w0928. Reason: edit
The topic has been locked.
  • w0928
  • w0928's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 5 months ago - 10 years 5 months ago #100069 by w0928
This is how I have it coded for a multiple choice question. I only have 2 answers: Yes (value of 1) and No (value of 2). There has got to be a better way of coding this, right?
Code:
<script type="text/javascript">
$(document).ready(function() {
$('#answer{SID}X{GID}X{QID}1').click(function (event) {
if ($( '#answer{SID}X{GID}X{QID}1' ).attr('checked') == true ) {
   $( '#answer{SID}X{GID}X{QID}2' ).attr('checked', false);
 
}
});
$('#answer{SID}X{GID}X{QID}2').click(function (event) {
if ($( '#answer{SID}X{GID}X{QID}2' ).attr('checked') == true ) {
   $( '#answer{SID}X{GID}X{QID}1' ).attr('checked', false);
}
});

EDIT: Also, it breaks my conditions for subsequent questions. I only show the next question if the answer to this one is yes. If they check yes it will show the next question. If they then click no, the yes will be unchecked but the next question still appears.
Last edit: 10 years 5 months ago by w0928.
The topic has been locked.
  • w0928
  • w0928's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 5 months ago #100077 by w0928
Anybody have a solution to this? I was able to get it working (conditions and all) on Chrome by changing the click functions to change functions and calling trigger('click') after setting the checked attribute to false but it doesn't work in IE at all.

So what I'd like to get working is to have 2 questions. The condition for question 2 is that the answer to question 1 is "Yes".

Basically...

Q1 Yes => change state from checked to not checked => yes is unchecked and question 2 is hidden
Q1 Yes => change state from not checked to checked => yes is checked, no is unchecked, and question 2 is shown
Q1 No => change state from checked to not checked => no is unchecked
Q1 No => change state from not checked to checked => no is checked, yes is unchecked, and question 2 is hidden
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose