How to concatenate values to the hdnfield value with comma on checkbox click event. when i click the checkbox i select order and passit to the JS function. If i select three checkboxes the hdnfield value should be like 1, 2, 3… something like that… how to do that?
if (chkBoxOne != null)
{
chkBoxOne.Attributes.Add("onclick", "javascript:return SelectOne('" + chkBoxOne.ClientID + "','" + e.Row.ClientID + "','" + lblorderId.Text + "')");
//if (chkBoxOne.Checked)
// hdSelectAllOrderId.Value += ((Label)e.Row.FindControl("lblorderId")).Text + ",";
}
function SelectOne(id, rowID, OrderID) {
var AllOrderIDs = 0;
AllOrderIDs = Number(document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value);
alert(AllOrderIDs);
if (document.getElementById(id).checked == true) {
if (AllOrderIDs == '')
AllOrderIDs = OrderID;
else
AllOrderIDs = AllOrderIDs + ' ,' + OrderID;
}
alert(AllOrderIDs);}
The above code is not working. when i click on the firt checkbox its showing frist ordid, but when i click on the second one its not showing first ordid which is i already assigned to it. Its just showing second one…
var AllOrderIDs = 0;
AllOrderIDs = document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value;
var IDs = AllOrderIDs.split(',');
if (document.getElementById(id).checked == true) {
if (IDs.indexOf(OrderID) == -1) {
IDs.push(OrderID);
}
}
else {
var index = IDs.indexOf(OrderID);
if (index != -1) {
IDs = IDs.slice(index, 1);
}
}
AllOrderIDs = IDs.join(',');
First of all, yes, you forgot to save the value to hidden field.
Second, you are only handling the situation, when the checkbox state changes to “checked”. thus, you don’t delete IDs from hidden field. If you check-uncheck-and-check-again, you will have duplicate ID saved.
I recommend you to split your string into array of IDs and then work with it.