INTRO
I have the following script that I want to use on a form that contains multiple questions that are answered with multiple checkbox inputs. I want to add checkbox values when checked to the corresponding textarea within the "alloptions" class.
JSFIDDLE http://jsfiddle.net/qFfMP/31/ This is how it works if there is only one set of checkboxes on a form. If you have two sets of checkboxes with the same class then selections are added to both textareas. I want to keep checkboxes checked within the alloptions class to only be posted to the nested textarea within that class.
QUESTION
How would I define a select event for "alloptions" as $(this) as this class defines inputs within? OR Would I better off defining "cb" as $(this) and reworking the code; which if I do not have to I rather not.
CODE
<div class="alloptions">
<textarea class="fulloptions" type="text" value=""></textarea><br>
<input type="checkbox" class="cb" value="Test1" />Test1<br>
<input type="checkbox" class="cb" value="Test2" />Test2<br>
<input type="checkbox" class="cb" value="Test3" />Test3
</div>
<hr>
<div class="alloptions">
<textarea class="fulloptions" type="text" value=""></textarea><br>
<input type="checkbox" class="cb" value="Test1" />Test1<br>
<input type="checkbox" class="cb" value="Test2" />Test2<br>
<input type="checkbox" class="cb" value="Test3" />Test3
</div>
<script>
$(document).ready(function () {
var checkboxes = $(".alloptions input[type='checkbox']");
checkboxes.on('change', function() {
$('.alloptions').find('.fulloptions').val(
checkboxes.filter(':checked').map(function(item) {
return this.value;
}).get().join(', ')
);
});
});
</script>