5

I have a tr column, like this. Then how can I get the tr's id using jquery after clicking Update having id "quick-update-order-product"

<tr id="product-id-<?=$product->product->id;?>">            
        <td><span class="product_price_show"></span></td>
        <td><span><?=  $product->product_qty; ?></span></td>
        <td><span><?= $product->product->stock->qty; ?></span></td>
        <td><span><?=  $product->total_price_tax_incl; ?></span></td>
        <td>
            <a class="quick-update-order-product">Update</a>                
        </td>            
</tr>

Thanks in advance.. :)

Anil Chaudhari
  • 801
  • 14
  • 30

3 Answers3

8

Firstly, if this row is repeated you should use a class on the button, not an id, as duplicate id properties are invalid.

<tr id="product-id-<?=$product->product->id;?>">            
    <td><span class="product_price_show"></span></td>
    <td><span><?=  $product->product_qty; ?></span></td>
    <td><span><?= $product->product->stock->qty; ?></span></td>
    <td><span><?=  $product->total_price_tax_incl; ?></span></td>
    <td>
        <a class="quick-update-order-product">Update</a>                
    </td>            
</tr>

Once you've got the class on the button you can use closest() to find the tr:

$('.quick-update-order-product').click(function() {
    var trId = $(this).closest('tr').prop('id');
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
3

You can use:

$('td a').click(function(){
  alert($(this).closest('tr').attr('id'));
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Use this Closest()

$('.quick-update-order-product').click(function() {
    var trId = $(this).closest('tr').attr('id');
});
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41