Below code snippet explains how to get all HTML input based on their name attribute using jQuery.
<input type="checkbox" id="Checkbox1" name = "chk" value = "1" />
<input type="checkbox" id="Checkbox2" name = "chk" value = "2" />
<input type="checkbox" id="Checkbox3" name = "chk" value = "3" />
<input type="checkbox" id="Checkbox4" name = "chk" value = "4" />
<input type="button" id="demo" value = "Demo" />
<script type = "text/javascript" src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$("#demo").live("click", function () {
$("input:checkbox[name=chk]").each(function () {
alert("Id: " + $(this).attr("id") + " Value: " + $(this).val() + " Checked: " + $(this).is(":checked"));
});
});
</script>
Explanation:
In the above example a click event handler has been assigned to the HTML input button. When the button is clicked, jQuery finds all the checkboxes with name “chk” using input:checkbox[name=chk] selector and then the Id, value and state of the all HTML input checkboxes are displayed in alert using jQuery.