I have bunch of checkboxes. I want to get values from them and pass it to span and hidden input. When I click one checkbox it works fine. But when I click more than one checkbox it pass me only last value. What's wrong? Please help me with that. And other question. Is it proper to use input or textarea to store multiple values?
function t() {
$('input[type="checkbox"]:checked').map(function(){
var dataAttr = $(this).attr('name');
var dataAttrVal = $(this).val();
$('input[name="' + dataAttr + '-value"]').val(dataAttrVal);
$('#summary_' + dataAttr).html(dataAttrVal);
console.log(dataAttr);
console.log(dataAttrVal);
}).get().join();
};
$('input[type="checkbox"]').on('click', function(){
t();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="sause" id="sause-garlic" value="Sos czosnkowy" class="sause">
<label for="sause-garlic">garlic</label>
<input type="checkbox" name="sause" id="sause-tabasco" value="Sos tabasco" class="sause">
<label for="sause-tabasco">tabasco</label>
<br>
<input type="checkbox" name="vegies" id="vegies-tomato" value="Tomato" class="vegies">
<label for="vegies-tomato">tomato</label>
<input type="checkbox" name="vegies" id="vegies-tomato" value="potato" class="vegies">
<label for="vegies-tomato">potato</label>
<input type="text" name="sause-value">
<input type="text" name="vegies-value">
<span id="summary_sause" class="summary"></span>
<span id="summary_vegies" class="summary"></span>