i have included a dynamic checkbox using rs.getstring
. i need to know which checkbox is checked.
<th>
<input type="checkbox" name="chkSingle_<%=counter %>" id ="chkSingle_<%= counter%>" value="<%=rs.getInt(11)%>">
</th>
i have included a dynamic checkbox using rs.getstring
. i need to know which checkbox is checked.
<th>
<input type="checkbox" name="chkSingle_<%=counter %>" id ="chkSingle_<%= counter%>" value="<%=rs.getInt(11)%>">
</th>
Try this
function getCheckedItems(checkboxName) {
var checkboxes = document.querySelectorAll('input[name="'+checkboxName+'"]:checked'), values = [];
Array.prototype.forEach.call(checkboxes, function(el) {
values.push(el.value);
});
return values;
}
You can call this function and pass the name of the checkbox, you can dynamically bind the onclick function to the button and provide the dynamic name you created while creating the checkbox.
Edited
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="checkbox" name="chkSingle" id ="chkSingle1" value="value1">
<input type="checkbox" name="chkSingle" id ="chkSingle2" value="value2">
<input type="checkbox" name="chkSingle" id ="chkSingle3" value="value3">
<button onclick="alert(getCheckedItems('chkSingle'))">Click</button>
<script type="text/javascript">
function getCheckedItems(checkboxName) {
var checkboxes = document.querySelectorAll('input[name="'+checkboxName+'"]:checked'), values = [];
Array.prototype.forEach.call(checkboxes, function(el) {
values.push(el.value);
});
return values;
}
</script>
</body>
</html>
Edited 2:
Try this method too.
function getCheckedItems(checkboxName) {
var checkboxes = document.getElementsByName(checkboxName);
var selected = [];
for (var i=0; i<checkboxes.length; i++)
{
if (checkboxes[i].checked)
{
selected.push(checkboxes[i].value);
}
}
return selected;
}
This edit has nothing to do with the question. This edit is on asker's request
When you are dynamically adding checkboxes, do this:
//this is your function I assume.
function toCreateCheckBoxesDynamically()
{
create checkboxes dynamically;
attach an attribute as onclick="function WhichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName')" to the button.
OR
//bind a click event to the button and call this function "function WhichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName')"
document.getElementById("yourButton").addEventListener("click", function()
{
whichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName');
});
}
// this is the function which you call on button click while submitting the form
function whichTakesCheckedValuesAndAttachValuesToHiddenFields(checkboxName)
{
var checkboxes = document.getElementsByName(checkboxName);
var selected = "";
for (var i=0; i<checkboxes.length; i++)
{
if (checkboxes[i].checked)
{
if(selected == "" || selected == 'undefined')
selected = checkboxes[i].value;
else
selected = selected +","+ checkboxes[i].value;
}
}
document.getElementById("hiddenFieldName").value = selected ;// the stored value is comma separated string so retrieve it on the server by using split method which will give you string array and then do your further work.
document.getElementById("yourFormName").submit(); // submit this form
}
hidden field syntax:
<input type=hidden name="yourHiddenFieldName" value="">// inside your form in html
OR one more tip
you can also save the dynamically created checkbox name in the hidden field in the form and after submitting get those checked values on the basis of that hidden field value as
String checkBoxName = request.getParameter('hiddenFieldName');
and then get all checkedBoxes values as
String checkedboxes[] = request.getParameterValues(checkBoxName);
Doing this you will get values of all checked boxes on the server side.
you can use $("input:checked[name^='chkSingle_']")
to get all checkboxes that are selected.
Try this code..
var ids = [],
cbx = document.querySelectorAll('input[type="checkbox"]');
for (i = 0, l = cbx.length; i < l; i++) {
ids.push(cbx[i].id);
}
console.log(ids);
If you want anymore help, jsfiddle is available