javascript – Jquery : how to know which row is selected? – Education Career Blog

I have a table with dynamically changing rows items.

Among the rows there is a small button / link for each unit.


   Data1 | LinkButton
   Data2 | LinkButton
   Data3 | LinkButton 
   Data4 | LinkButton
   Data5 | LinkButton 
   Data6 | LinkButton

What i want is that when i click on the link button ,i need to know which row is selected?Can any one give me pointers on how that can be done?

You help will be greatly appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="css/main.css" type="text/css" media="screen, projection" rel="stylesheet" />

        <div id="message" style="display: none;">
        <div id="waiting" style="display: none;">
            Please wait<br />
            <img src="images/ajax-loader.gif" title="Loader" alt="Loader" />
        <form action=""  id="searchForm">
            <label for="search">Matter Search:</label> <input type="text" name="search" id="search" />
            <input type="submit" id="submit" value="Submit" />
            <a href="#">Link</a>

        <div id="matterTableDiv"  style="display: none;">
            List of Matters
            <table id="matterTable"   border="1">
                <th>Matter Names</th>
                <th>Matter Number</th>


        <div id="matterDetailTableDiv" style="display: none;">
            Matter Detail Table
        <script type="text/javascript" src="js/json2.js"></script>
        <script type="text/javascript" src="js/jquery-1.4.2.js"></script>

        <script type="text/javascript">




                    $("#matterTable tbody").text("");
                        type : 'POST',
                        url : 'post.php',
                        dataType : 'json',
                        data: {
                            search : $('#search').val()
                        success : function(data){
                            if(data.msg == "" ){
                                .text('There was an error.').show(200);


                            $('#matterTableDiv').removeClass().addClass((data.error === true) ? 'error' : 'success')
                            if(data.error == false){

                                var str = JSON.parse(data.msg);
                                //alert("List :" + str.length);
                                //alert("List :" + str.toString());

                                $.each(str, function(key, value) {
                                    var tblRow =
                                        //+"<td><a id="+key+" href='#dbID="+value.dbID+"&matID="+value.matterID+">"+value.matterInfoSortName+"</a></td>"
                                        +"<td><input type='button' value="+value.matterInfoFileDesc+"></td>"
                                    $(tblRow).appendTo("#matterTable tbody");
                                    //alert(key + ': ' + value.matterInfoSortName);


                                     var row = $(this).closest("tr");
                                     var rowIndex = row.index();

                                .text('There was an error.').show(200);


                            if (data.error === true)
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            .text('There was an error.').show(200);

                    return false;





I know that you ask for the row index, there are users that have given you the answer.
But, usually we need the id of the row, because the id belongs to an id in the database.
In this case, you could use the id of the cell or the button itself.

   <tr  id="Data1"><td>a</td><td>b</td><td>c</td><td><input type="button" value="Data"/></td></tr>
    <tr  id="Data2"><td>a</td><td>b</td><td>c</td><td><input type="button" value="Data"/></td></tr>
    <tr  id="Data3"><td>a</td><td>b</td><td>c</td><td><input type="button" value="Data"/></td></tr>
    <tr  id="Data4"><td>a</td><td>b</td><td>c</td><td><input type="button" value="Data"/></td></tr>

    $("inputtype=button").live("click", function() {
       var row = $(this).closest("tr");
       var rowId = row.attr("id");

You have this here:


You can go from this (the clicked button) in the handler and use .closest() to get the <tr> of the button then grab anything you want from there, for example:

$(".someButton").live("click", function() {
   var row = $(this).closest("tr");
   var rowIndex = row.index();

For a full list of “moving-around” functions like this, check out the Tree Traversal section of the API.


I am guessing you are using…


So in that case, you can simply find it from inside some_function like so:

function some_function() {
    $(this).parents('tr'); // this will give you the link's row.

Leave a Comment