mohsen kholaseshoar mohsen kholaseshoar - 4 months ago 29
Javascript Question

How to insert value of checked checkbox in array using jQuery

I want select all input values that is

checked
and has name
DisbursementUnitCodes


$('input:checked[name=DisbursementUnitCodes]').val()


This code give the first element value but i want give all input value to list or array.

Answer

Use .each() to iterating selected elements and get value of its.

var arr = [];
$("input:checked[name=DisbursementUnitCodes]").each(function(){
    arr.push($(this).val());
});
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="DisbursementUnitCodes" value ="A" checked />
<input type="checkbox" name="DisbursementUnitCodes" value ="B" />
<input type="checkbox" name="DisbursementUnitCodes" value ="C" checked />
<input type="checkbox" name="other" value ="D" checked />
<input type="checkbox" name="other" value ="E" />