ChandaMAMA
ChandaMAMA

Reputation: 73

Get selected row id

I am using below jquery to get selected row id

<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
      <script type="text/javascript">
          //MainContent_tbl
          $(document).ready(function () {
              $('#MainContent_tbl tr').click(function (event) {
                  alert(this.id); //trying to alert id of the clicked row          

              });
          });



    </script>

My problem is that my table(MainContent_tb) is not static, i need to pass this as variable, any suggestion or remarks would be appreciated.

Upvotes: 2

Views: 8787

Answers (4)

ShankarSangoli
ShankarSangoli

Reputation: 69905

You can use delegate and attach click handler on the document with selector as tr. This way there will be only one click handler attached and also you don't have to bother about table id. It will work for any table. Try this.

      $(document).ready(function () {
          $(document).delegate('tr', 'click', function (event) {
              alert(this.id); //trying to alert id of the clicked row          
          });
      });

Reference: .delegate()

Upvotes: 0

Jannis M
Jannis M

Reputation: 745

  $(document).ready(function () {
    $('#MainContent_tbl tr').click(function (event) {
        var elID = $(this).attr('id');
        alert(elID);
    });
  });

This is what you are looking for!

Upvotes: 1

dku.rajkumar
dku.rajkumar

Reputation: 18568

try this

  $('#'+table_id_as_variablle+' tr').click(function (event) {
       alert(this.id); //trying to alert id of the clicked row          
  });

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

Something like

var tableId = 'MainContent_tbl';
$('#' + tableId + ' tr').click(function(event) {
    alert(this.id);
}

should work. It simply builds the selector string using the variable, rather than being hardcoded.

Upvotes: 1

Related Questions