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
|
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. |
|||
|
|||
7 comments :
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 :)
Hi...Suresh this is very helpful...Great Job Man...Hats Off To You....Well Done Suresh
Nice... Post.. can you help in validation form for contact us page
nice post suresh..
Hi,
Suresh. Nice post. For more detailed validation controls Please check my blog.
http://aspdotnetchamp.blogspot.in/
Nyc post dude
Note: Only a member of this blog may post a comment.