0

I have a grid view and I want to see if the checkbox in the first column is checked. If the check box is checked it opens a new window. I cannot figure out how to see if the checkbox is checked. Please help the function below is not working and I can't figure out why.

function mapSelectedClick() 
    {
        var CustomerIDs = "";
        var grid = document.getElementById('<%=grdCustomers.ClientID %>');

        for (var i = 1; i < grid.rows.length; i++)
        {
            var Row = grid.rows[i];
            var CustomerID = grid.rows[i].cells[1].innerText;
            if (grid.rows[i].cell[0].type == "checkbox")
            {
                if (grid.rows[i].cell[0].childNodes[0].checked)
                {
                    customerIDs += CustomerID.toString() + ',';
                }
            }
        }

        customerIDs = customerIDs.substring(0, customerIDs.length-1);
        window.open("MapCustomers.aspx?CustomerIDs=" + customerIDs);
    }

1 Answer 1

1

There are few problems in your code:

  1. In both if conditions, you used cell[0] instead of cells[0]
  2. In outer if condition you used cell[0].type == "checkbox". Cell can't have type checkbox, it includes chekbox control as its child

Modify your function like this:

function mapSelectedClick() 
{
    var CustomerIDs = "";
    var grid = document.getElementById('<%=grdCustomers.ClientID %>');

    for (var i = 1; i < grid.rows.length; i++)
    {
        var Row = grid.rows[i];
        var CustomerID = Row.cells[1].innerText;
        var ctrl = Row.cells[0].childNodes[0];
        if (ctrl.type == "checkbox")
        {
            if (ctrl.checked)
            {
                customerIDs += CustomerID.toString() + ',';
            }
        }
    }

    customerIDs = customerIDs.substring(0, customerIDs.length-1);
    window.open("MapCustomers.aspx?CustomerIDs=" + customerIDs);
}

Note that innerText is not cross browser compatible, use innerHTML wherever possible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.