jeudi 20 décembre 2018

Checkbox returns the one value even after everything is unselected

I have a small query. Here I have 5 checkboxes and I want to display the value of the selected checkbox in a input tag. Everything is working fine however whenever I uncheck all the boxes, The input always returns the value of the last unchecked checkbox. What I want is when I uncheck all the checkboxes, The input tag should be empty. I have tried 'if' statement to clear the input when every box is empty but it sometimes work unexpectedly.I am using jQuery as a javascript library. Kindly guide. Thank you.

Check Example value 1 value 2 value 3 value 4 value 5
<input type="text" id="checkVal">

<script>
    $('.checkbox_ss').click(function(){
        var text = '';
        $('.checkbox_ss:checked').each(function(){
            text += $(this).val();
            $('#checkVal').val(text);
        });
    });
</script>




Aucun commentaire:

Enregistrer un commentaire