posted 6/27/2009 by Raghav Khunger
In this blog I will show how to get or find all input tags of type checkbox with jQuery. In many scenarios we need to traverse all the controls of particular type so below is the source code for it :
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> </head> <body> <form runat="server"> <input id="TestControl1" type="checkbox" value="Value1" /> <br /> <input id="TestControl2" type="checkbox" value="Value2" /> <br /> <input id="TestControl3" type="checkbox" value="Value3" /> <br /> <input id="TestButton" type="button" value="Get Checkbox Controls" /> </form> </body> <script type="text/javascript"> $('#TestButton').click(function (e) { var checkboxControls = $('input[type=\'checkbox\'] '); $.each(checkboxControls, function (i, checkboxControl) { alert(checkboxControl.value); }); }); </script> </html>
Above I have used three checkboxes and one input button. On click of button we need to traverse the entire checkboxes and get their values so I have used this code to achieve it:
var checkboxControls = $('input[type=\'checkbox\'] '); $.each(checkboxControls, function (i, checkboxControl) { alert(checkboxControl.value); });
Do let me know your feedback, comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18