-2

I am trying to get the value of table row using jQuery. When I try the same jQuery code on console and after I try to click, that is selecting that row and give proper value what I expect. But I don't know why it is not running from my JavaScript file. But the other functions are working on the same JavaScript file

$(".chequeTable tbody tr").click(function() {
  alert('Vendor ID ==');
  $('.selected').removeClass('selected');
  $(this).addClass("selected");

  alert('Vendor ID ==' + $('.tdVendorID').html());

  $(this).css("background-color", "#000000");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover FilterTable chequeTable table-striped" id="queryTable">
  <thead>
    <tr>
      <th>S.No</th>
      <th>VendorID</th>
      <th>VendorName</th>
      <th>ReqNo</th>
      <th>ChequeAmount</th>
      <th>VendorBalance</th>
      <th>Balance</th>
      <th>Description</th>
      <th>StockValue</th>
      <th>Yes/No</th>
    </tr>
  </thead>
  <tbody>
    <tr class="">
      <td class="tdSno">1</td>
      <td class="tdVendorID">6000 </td>
      <td class="tdVendorName">john</td>
    </tr>
  </tbody>
</table>
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Liam neesan
  • 1,783
  • 4
  • 23
  • 49

2 Answers2

3

Most probably the table is not there when you initialize the click event in jquery. Do you build the table dynamically?

Try to bind the click event to the table instead of the tr directly:

$(".chequeTable").on("click", "tr", function() { ... });

This only works when the table.chequeTable is in the HTML.

Some explanation why:
jquery documentation for on()

It says that it binds the events to all the elements in the selector. That means, elements that are not in the selector (any tr that is not yet present for example) won't receive the event. For that you have to bind the click Event on a container and check what the clicked subelement is. This is more perfomant anyways because you only attach one click Event and not 100s (if you have 100s tr-Elements)

cloned
  • 4,562
  • 3
  • 18
  • 31
0

Here you go with a solution

$("table.chequeTable").on('click', 'tr', function() {
  alert('Vendor ID ==');
  $('.selected').removeClass('selected');
  $(this).addClass("selected");

  alert('Vendor ID ==' + $('.tdVendorID').html());

  $(this).css("background-color", "#000000");
});

$('tbody').append(`<tr class="">
      <td class="tdSno">1</td>
      <td class="tdVendorID">6000 </td>
      <td class="tdVendorName">john</td>
    </tr>`);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover FilterTable chequeTable table-striped" id="queryTable">
  <thead>
    <tr>
      <th>S.No</th>
      <th>VendorID</th>
      <th>VendorName</th>
      <th>ReqNo</th>
      <th>ChequeAmount</th>
      <th>VendorBalance</th>
      <th>Balance</th>
      <th>Description</th>
      <th>StockValue</th>
      <th>Yes/No</th>
    </tr>
  </thead>
  <tbody>
    
  </tbody>
</table>

You need to do event delegation as your row are generated dynamically.

Reference Documentation: Event delegation

Hope this will help you.

Shiladitya
  • 11,210
  • 15
  • 22
  • 33