get RowIndex of Grid from a checkbox

-1

I'm trying to get the row index of my checkbox, the current code i can get the checked value of the checkbox. the closest doesn't work and i'm stack. If i check a box on grid row 1 an alert should shows 1

        $(":checkbox").change(function () {
            if ($(this).is(':checked')) {
                var id = $(this).closest("tr");

                alert(id.innerText);


            } else {
                alert("Failed");
            }
        });

EDIT: iv'e added this line

var $id = $(this).closest("tr");
   alert($id.index() + 1)
   var table = document.getElementById("tblSearchLogs");
   var appid = table.rows[$id].cells[1].innerText

But it gives me an error

0x800a138f - JavaScript runtime error: Unable to get property 'cells' of undefined or null reference

TABLE DIV

  <div class="row" style="margin-top: 1em;">
            <div  class="table-responsive">
                <table id="tblSearchLogs" class="table table-bordered table-striped">
                    <thead>

                        <tr >
                            <th style="width: 5%; font-weight:bold"> </th>
                            <th style="width: 10%; font-weight:bold">No.</th>
                            <th style="width: 30%; font-weight:bold">App ID</th>
                            <th style="width: 30%; font-weight:bold">Name</th>
                            <th style="width: 40%; font-weight:bold">Date of Birth</th>


                        </tr>
                    </thead>
                    <tbody>

                        @{
                            if (Model != null)
                            {
                                int x = 1;
                                foreach (xxxx.Models.Class.Listxx ldx in Model.searchlist)
                                {

                                    <tr>
                                        <td> <input type="checkbox" class="divChckBox"/> </td>
                                        <td>@x </td>
                                        <td id="appid">@ldx.AppID</td>
                                        <td>@ldx.Name </td>
                                        <td>@ldx.DOB</td>



                                    </tr>
                                    x++;
                                }

                            }

                        }
                    </tbody>
                </table>
            </div>
jquery
asked on Stack Overflow Dec 28, 2017 by user3751759 • edited Dec 28, 2017 by user3751759

1 Answer

0

InnerText is for getting and setting text property of element. Also its not working here as you are calling it on jquery object of element and not DOM element.

You need to use index of element here:

 var $id = $(this).closest("tr");
 alert($id.index() + 1); // as index starts from 0
answered on Stack Overflow Dec 28, 2017 by Milind Anantwar • edited Dec 28, 2017 by Milind Anantwar

User contributions licensed under CC BY-SA 3.0