user3241020
user3241020

Reputation: 37

How to get the id of the last element?

I have the list of products ,using below code

<table id="product">
    <thead>
        <tr> 
            <th style="width:30%;">Item Name</th>
            <th style="width:11%;">Price</th>
            <th style="width:11%;">Qty</th>
        </tr>
    </thead>
    <tbody id="cart_contents">
        <?php $i=1;
            foreach($cart as $line=>$item)
            {
            ?>
                <tr>
                    <td style="align:center;"><?php echo $item['name']; ?></td>
                    <td><input type="text"  id="price_<?php echo $i;?>" value="<?php echo $item['price']; ?>" name="price"></td>
                    <td id="qnty"><input type="text"  id="quantity_<?php echo $i;?>" value="<?php echo $item['quantity']; ?>" name="quantity"></td>
                </tr>
            <?php 
                $i++;
            }
        ?>
    </tbody>
</table>

I need to edit quantity and price fields of last added product.

<table width="297px" style="float:left" class="CSSTableGenerator">
    <tr>
        <td onclick="click_quantity();"><a href="javascript:void(0);">Qty</a></td>      
        <td onclick="click_price();"> <a href="javascript:void(0);" >Price</a></td>
    </tr>
</table>
function click_quantity(){   
    $("#table register").find("td qnty").each(function(){
        var id = $(this).attr("id");
        alert(id);
    }) 
}

How to get the last quantity field's id, after clicking the Qty link?

I have tried some codes, but I am unable to get last element's id.

EX : id names are quantity_1, quantity_2, quantity_3, quantity_4.

How to get quantity_4 (last elment's id)?

Upvotes: 1

Views: 1790

Answers (6)

Alex
Alex

Reputation: 457

$("#cart_contents td:last")

using this you'll find last column in each row so you have to use

$("#cart_contents tr:last-child td.qnty")

Upvotes: 0

Sauryabhatt
Sauryabhatt

Reputation: 269

HTML

<table id="product">
<thead>
<tr> 
    <th style="width:30%;">Item Name</th>
    <th style="width:11%;">Price</th>
    <th style="width:11%;">Qty</th>
</tr>
</thead>
<tbody id="cart_contents">
<?php $i=1;
foreach($cart as $line=>$item)
{
?>
<tr>
    <td style="align:center;"><?php echo $item['name']; ?></td>
    <td>
        <input type="text"  id="price_<?php echo $i;?>" value="<?php echo $item['price']; ?>" name="price"/>
    </td>
    <td class="qnty">
         <input class="qnty_input" type="text"  id="quantity_<?php echo $i;?>" value="<?php echo $item['quantity']; ?>" name="quantity"/>
    </td>
</tr>
 <?php 
 $i++;
  } ?>
</tbody>
</table>

JS Code

  function click_quantity(){
    var id = $("#cart_contents tr:last-child .qnty_input").attr("id");
    // rest of your logic
  }

Upvotes: 2

sshet
sshet

Reputation: 1160

In order to get last child element you can make use of Try to use last property of jquery

$("#cart_contents td:last").find('input[type=text]').attr('id');

Upvotes: 0

KarthikManoharan
KarthikManoharan

Reputation: 805

To get the last row(td) in table you can use this,

$('table tbody tr td:last-child')

Upvotes: 0

pratik nagariya
pratik nagariya

Reputation: 574

Try this one.

var last_id = $( 'table tbody tr td:last-child').attr( 'id' );

Upvotes: 0

Justinas
Justinas

Reputation: 43441

1) you are using same ID in loop, that is not valid;

2) I think your js is wrong, use : $("#register").find("#qnty"), but has to be find('.qnty')

Upvotes: 0

Related Questions