dimanche 20 mars 2016

How to define $(this) to add checkbox values to textarea

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.

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>




Aucun commentaire:

Enregistrer un commentaire