show-notice
hide-notice

Tuesday, 2 July 2013

jQuery Get Selected Checkbox Values with Comma Separated List on Name




Introduction

Here I will explain how to use jQuery to get checked / selected checkbox values with comma separated list based on name.



Get selected checkbox values in jQuery

To get selected checkbox values based on name we need to write the code like as shown below


var slvals = []
$('input:checkbox[name=nameofyourcheckbox]:checked').each(function() {
slvals.push($(this).val())
If you want to see it in complete example check below code


<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btnSubmit').click(function() {
var slvals = []
$('input:checkbox[name=chkcountry]:checked').each(function() {
slvals.push($(this).val())
})
alert('Selected Checkbox values are: ' + slvals)
})
});
</script>
</head>
<body>
<b>Sample checkbox inputs :</b>
<input type="checkbox" name="chkcountry" id="chkindia" value="India" />India
<input type="checkbox" name="chkcountry" id="chkusa" value="USA" />USA
<input type="checkbox" name="chkcountry" id="chkAustralia" value="Australia" />Australia
<input type="checkbox" name="chkcountry" id="chkMalaysia" value="Malaysia" />Malaysia
<button id="btnSubmit">Get Selected Values</button>
</body>
</html>


SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com