- Posts: 16
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
- mktfan
- Topic Author
- Visitor
10 years 8 months ago - 10 years 8 months ago #99048
by mktfan
Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others was created by mktfan
Hi,
I need your help with a javascript code.
I have an array question with checkboxes. I am trying to exclude all other options in a row if the user the last one. I've this article where you can find a javascript code to do it:
manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others
The code proposed is:
I've used it, but something weird happens. Please, have a look to my survey here:
enquestas.com/sv/index.php/333559/lang-es
Code only works in the last row, but not in the 2 first one.
I am using Limesurvey V2.0
Can you help me, please?
Many thanks in advance
Regards
Víctor
I need your help with a javascript code.
I have an array question with checkboxes. I am trying to exclude all other options in a row if the user the last one. I've this article where you can find a javascript code to do it:
manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others
The code proposed is:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclude function using question ID(s) excludeOpt (QQ); // A function to make the last option in each array row exclusive function excludeOpt (qID) { // Add some classes to the checkboxes so we can manipulate them $('#question'+qID+' table.question tbody td').addClass('normalOpt'); $('#question'+qID+' table.question tbody').each(function(i) { $('td:last', this).removeClass('normalOpt').addClass('exlusiveOpt') }); // A listener on the checkbox cells $('#question'+qID+' table.question tbody td').click(function (event) { // Set some vars var el = $(this).parent(); var optLength = $('td', el).length // Uncheck the appropriate boxes in a row if ($(this).hasClass('normalOpt')) { $('td:last input[type=checkbox]', el).attr('checked', false); } else { $('td', el).each(function(i) { if (i < (optLength - 1)) { $('input[type=checkbox]', this).attr('checked', false); } }); } }); // A listener on the checkboxes $('#question'+qID+' table.question tbody td input[type=checkbox]').click(function (event) { // Set some vars var el2 = $(this).parent().parent(); var optLength = $('td', el2).length // Uncheck the appropriate boxes in a row if ($(this).parent().hasClass('normalOpt')) { $('td:last input[type=checkbox]', el2).attr('checked', false); } else { $('td', el2).each(function(i) { if (i < (optLength - 1)) { $('input[type=checkbox]', this).attr('checked', false); } }); } }); } }); </script>
I've used it, but something weird happens. Please, have a look to my survey here:
enquestas.com/sv/index.php/333559/lang-es
Code only works in the last row, but not in the 2 first one.
I am using Limesurvey V2.0
Can you help me, please?
Many thanks in advance
Regards
Víctor
Last edit: 10 years 8 months ago by mktfan. Reason: faltaba URL
The topic has been locked.
- w0928
- Offline
- New Member
Less
More
10 years 7 months ago #99988
by w0928
Replied by w0928 on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
I'm having the same problem. Can someone please post the code that actually works? I've tried manipulating the code myself but I am having no luck whatsoever.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
10 years 7 months ago #100005
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Try this. Note that if using 1.92+, you do not need to replace "{QID}" - this tag will automatically insert the question ID:
I have updated the workaround - manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclude function using question ID excludeOpt ({QID}); }); // A function to make the last option in each array row exclusive function excludeOpt (qID) { var thisQuestion = $('#question'+qID) // Add some classes to the checkbox cells $('table.question tbody td', thisQuestion).addClass('normalOpt'); $('table.question tbody tr', thisQuestion).each(function(i) { $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt') }); // A listener on the checkbox cells $('table.question tbody td', thisQuestion).click(function (event) { // Set some vars var thisRow = $(this).closest('tr'); // Uncheck the appropriate boxes in a row if ($(this).hasClass('normalOpt')) { $('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false); } else { $('.normalOpt input[type=checkbox]', thisRow).attr('checked', false); } }); // A listener on the checkboxes $('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) { // Set some vars var thisRow = $(this).closest('tr'); var thisCell = $(this).closest('td'); // Uncheck the appropriate boxes in a row if ($(thisCell).hasClass('normalOpt')) { $('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false); } else { $('.normalOpt input[type=checkbox]', thisRow).attr('checked', false); } }); } </script>
I have updated the workaround - manual.limesurvey.org/Workarounds:_Manip..._Excludes_All_Others
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: w0928
The topic has been locked.
- MVT
- Offline
- Junior Member
Less
More
- Posts: 24
- Thank you received: 0
9 years 5 months ago - 9 years 5 months ago #114688
by MVT
Replied by MVT on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Hi,
I tried this but...
On some lines, the last column is "unchecked" after one or more other check-boxes checked.
I've a check-box matrix where the responses are mandatory.
I tried your snippet alone and with one other (check one column by default to avoid validation problems, I mean I've add a column on the right named "N/A" (NSP in French), and this colomn is checked by default on load.
It should be easier with the test survey.
Limesurvey Release : 205plus-build141113
May be I did something wrong ?
Thanks for your help
<EDIT>
because of my insufficient knowledge in javascript, I suppose I did somme errors.
I've corrected the script in order to make it more "correct" but it doesn't work well
I tried this but...
On some lines, the last column is "unchecked" after one or more other check-boxes checked.
I've a check-box matrix where the responses are mandatory.
I tried your snippet alone and with one other (check one column by default to avoid validation problems, I mean I've add a column on the right named "N/A" (NSP in French), and this colomn is checked by default on load.
It should be easier with the test survey.
Limesurvey Release : 205plus-build141113
May be I did something wrong ?
Thanks for your help
<EDIT>
because of my insufficient knowledge in javascript, I suppose I did somme errors.
I've corrected the script in order to make it more "correct" but it doesn't work well
Code:
< script type = "text/javascript" charset = "utf-8" > $(document).ready(function () { // Call the function with a question ID and column number Once var wQuit = true; if (wQuit) { checkedDefault({ QID }, 12); wQuit = false; }; // Call the exclude function using question ID excludeOpt({ QID }); }); // A function to pre-check a column of an array function checkedDefault(qID, column) { var checkedCol = column - 1; $('#question' + qID + ' tr.subquestions-list').each(function (i) { if ($('input.checkbox:checked', this).length == 0) { $('input.checkbox:eq(' + checkedCol + ')', this).prop('checked', true); $('input.checkbox:eq(' + checkedCol + ')', this).parent().find('input[type="hidden"]').val(1); } }); } // A function to make the last option in each array row exclusive function excludeOpt(qID) { var thisQuestion = $('#question' + qID) // Add some classes to the checkbox cells $('table.subquestions-list tbody td', thisQuestion).addClass('normalOpt'); $('table.subquestions-list tbody tr', thisQuestion).each(function (i) { $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt') }); // A listener on the checkbox cells $('table.subquestions-list tbody td', thisQuestion).click(function (event) { // Set some vars var thisRow = $(this).closest('tr'); // Uncheck the appropriate boxes in a row if ($(this).hasClass('normalOpt')) { $('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false); } else { $('.normalOpt input[type=checkbox]', thisRow).attr('checked', false); } }); // A listener on the checkboxes $('table.subquestions tbody td input[type=checkbox]', thisQuestion).click(function (event) { // Set some vars var thisRow = $(this).closest('tr'); var thisCell = $(this).closest('td'); // Uncheck the appropriate boxes in a row if ($(thisCell).hasClass('normalOpt')) { $('.exlusiveOpt input[type=checkbox]', thisRow).attr('checked', false); } else { $('.normalOpt input[type=checkbox]', thisRow).attr('checked', false); } }); } < / script >
Attachments:
Last edit: 9 years 5 months ago by MVT. Reason: modifications
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
9 years 5 months ago - 9 years 5 months ago #114732
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Okay, so you want the last column to be checked by default and the last column is also exclusive.
Here's a simplified script to do that:
And a working 2.05 survey:
Here's a simplified script to do that:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); //// Check the last option in each row by default //// $('tr.subquestions-list', thisQuestion).each(function(i) { if ($('input.checkbox:checked', this).length == 0) { $('input.checkbox:last', this).prop('checked', true); $('input.checkbox:last', this).parent().find('input[type="hidden"]').val(1); } }); //// Make the last option in each array row exclusive //// // Add some classes to the checkbox cells $('table.subquestions-list tbody td', thisQuestion).addClass('normalOpt'); $('table.subquestions-list tbody tr', thisQuestion).each(function(i) { $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt') }); // A listener on the checkboxes $('input[type=checkbox]', thisQuestion).change(function (event) { var thisRow = $(this).closest('tr'); var thisCell = $(this).closest('td'); if ($(this).is(':checked') && thisCell.hasClass('normalOpt')) { $('.exlusiveOpt input[type=checkbox]', thisRow).prop('checked', false); $('.exlusiveOpt input[type="hidden"]', thisRow).val(''); } if ($(this).is(':checked') && thisCell.hasClass('exlusiveOpt')) { $('.normalOpt input[type=checkbox]', thisRow).prop('checked', false); $('.normalOpt input[type="hidden"]', thisRow).val(''); } }); }); </script>
And a working 2.05 survey:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 9 years 5 months ago by tpartner.
The following user(s) said Thank You: MVT
The topic has been locked.
- MVT
- Offline
- Junior Member
Less
More
- Posts: 24
- Thank you received: 0
9 years 5 months ago #114743
by MVT
Replied by MVT on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Ok, thanks.
I'll try it on my new survey.
I've to redesign it in order to simplify the job for my respondents... if I want to have answers !
The best way should be to chain surveys in a conditional mode, I mean, to show the first part (participation after inscription), save some data and, at the end of the first survey, send an invitation for the second part (survey) with some data completed. But it is for "the future".
This first snippet should simplify the job for my first survey.
Regards
Michel
I'll try it on my new survey.
I've to redesign it in order to simplify the job for my respondents... if I want to have answers !
The best way should be to chain surveys in a conditional mode, I mean, to show the first part (participation after inscription), save some data and, at the end of the first survey, send an invitation for the second part (survey) with some data completed. But it is for "the future".
This first snippet should simplify the job for my first survey.
Regards
Michel
The topic has been locked.
- MVT
- Offline
- Junior Member
Less
More
- Posts: 24
- Thank you received: 0
9 years 4 months ago #115493
by MVT
Replied by MVT on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Hi tpartner,
Sorry, I'm very late... I've to redesign my survey (reorder the questions... to put the questions that need more reflection at the beginning and the easiest ones at the end of the survey)
Your script works very well.
For the respondents, I've activated the save option. It should be better for them to have a look at the "pdf" release before answering the survey on line. So I hope I will not have to split the survey in 3 parts and have to look at a complex (for me) javascript management !
Thanks
Sorry, I'm very late... I've to redesign my survey (reorder the questions... to put the questions that need more reflection at the beginning and the easiest ones at the end of the survey)
Your script works very well.
For the respondents, I've activated the save option. It should be better for them to have a look at the "pdf" release before answering the survey on line. So I hope I will not have to split the survey in 3 parts and have to look at a complex (for me) javascript management !
Thanks
The topic has been locked.
- AndrejL
- Offline
- New Member
Less
More
- Posts: 11
- Thank you received: 0
8 years 11 months ago #120467
by AndrejL
Replied by AndrejL on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Hi!
I would like to alter the script from the first post to make exclusive last 2 (not just last one) elements in each array row. It sounded simple at first but as I'm no javascript expert I don't know how to do it myself.
Does anybody know how I can change the code to make it work?
Thanks,
Andrej
I would like to alter the script from the first post to make exclusive last 2 (not just last one) elements in each array row. It sounded simple at first but as I'm no javascript expert I don't know how to do it myself.
Does anybody know how I can change the code to make it work?
Thanks,
Andrej
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 11 months ago #120474
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Try this to make the last two columns exclusive:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclude function using question ID excludeOpt ({QID}); }); // A function to make the last option in each array row exclusive function excludeOpt (qID) { var thisQuestion = $('#question'+qID) // Add some classes to the checkbox cells $('table.question tbody td', thisQuestion).addClass('normalOpt'); $('table.question tbody tr', thisQuestion).each(function(i) { // Last two coluns are exclusive $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); }); // A listener on the checkboxes $('table.question tbody td input[type=checkbox]', thisQuestion).change(function (event) { if($(this).is(':checked')) { // Set some vars var thisRow = $(this).closest('tr'); var thisCell = $(this).closest('td'); // Uncheck the appropriate boxes in the row if ($(thisCell).hasClass('normalOpt')) { // Non-exclusive $('.exlusiveOpt input[type=checkbox]', thisRow).each(function(i) { $(this).prop('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } else { // Exclusive $('input[type=checkbox]', thisRow).not(this).each(function(i) { $(this).prop('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } } }); } </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- AndrejL
- Offline
- New Member
Less
More
- Posts: 11
- Thank you received: 0
8 years 11 months ago #120480
by AndrejL
Replied by AndrejL on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
I'm afraid it's not working. Now even last answer is not exclusive
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 11 months ago #120483
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
What version of LimeSurvey are you using? Please give a link to a small test survey.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- AndrejL
- Offline
- New Member
Less
More
- Posts: 11
- Thank you received: 0
8 years 11 months ago - 8 years 11 months ago #120485
by AndrejL
Replied by AndrejL on topic Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others
Of course.
We are using 2.00+,. And can not upgrade at this point.
I exported just this question here zadovoljstvo.sparslovenija.si/limesurvey...x.php/784886/lang-sl
First example uses first script and second one uses modified script.
We are using 2.00+,. And can not upgrade at this point.
I exported just this question here zadovoljstvo.sparslovenija.si/limesurvey...x.php/784886/lang-sl
First example uses first script and second one uses modified script.
Last edit: 8 years 11 months ago by AndrejL.
The topic has been locked.