Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Get Checked / Selected Checkbox Values with Comma Separated List Based on Name

Jun 24, 2013
Introduction

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

Description:

In previous articles I explained jQuery Get Set Delete Cookie example, jQuery Show gridview row details on mouseover, jQuery Create simple mobile website, jQuery clear textbox value on focus, jQuery Draggable & Resizable div with example and many articles relating to JQuery, JavaScript, asp.net, code snippets. Now I will explain how to get selected checkbox values with comma separated list based on name in jQuery.

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


<html>
<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>
Live Demo

Sample checkbox inputs : India USA Australia Malaysia


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

7 comments :

sanju said...

This is very nice article, but within this example can u explain me how do i postback to codebehind either to json or javascript. your suggestion will be appreciated. Thank u :)

Azeem said...

Hi...Suresh this is very helpful...Great Job Man...Hats Off To You....Well Done Suresh

candyfredric said...

Nice... Post.. can you help in validation form for contact us page

Anonymous said...

nice post suresh..

DotNet said...
This comment has been removed by the author.
Francis said...

Hi,
Suresh. Nice post. For more detailed validation controls Please check my blog.
http://aspdotnetchamp.blogspot.in/

Manjeet Singh said...

Nyc post dude

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.