Claremont
Claremont

Reputation: 345

.text() the parent class

I am trying to change .cartSubPrice text to the new price provided by the JSON request. Everything else works perfectly even the total price change and I know it is returned because of firebug and just alert(resultSub) shows it. I just cant figure out how to tell it to go to the proper place and change. Any help would be greatly appreciated.

Jquery

$('.cartUpdate').bind("keyup", function() {
    changeItem = $(this);
    var url = 'cart/update' 
    var qty = $(this).attr('value');
    var rowid = $(this).attr('name');

    if(qty > 0) {
        $.post(url, {qty: qty, rowid: rowid }, function changeCost () {     

            $.getJSON('cart/updatePrice', {rowid: rowid}, function(data) {                  
                //convert to currency                                   
                resultSub = data.subTotal.toFixed(2);
                resultTotal = data.totalPrice.toFixed(2);           

                //change
                $(changeItem).parent('.cartSubPrice').text(resultSub);      
                $('.cartTotalPrice').text(resultTotal);


            });

        });
    }   

    return false;
});

Html

<div class="cartItem">
    Item 1
    <div class="cartSubPrice">80.00</div>    
    <input type="text" name="76ea881ebe188f1a7e7451a9d7f17ada" value="4" class="cartUpdate" >   
    <a href="#" id="cartDelete" name="76ea881ebe188f1a7e7451a9d7f17ada">x</a>
</div>
<div class="cartItem">
    Item 2
    <div class="cartSubPrice">20.00</div>    
    <input type="text" name="e7a36fadf2410205f0768da1b61156d9" value="1" class="cartUpdate" >   
    <a href="#" id="cartDelete" name="e7a36fadf2410205f0768da1b61156d9">x</a>
</div>

Upvotes: 0

Views: 293

Answers (3)

nicosantangelo
nicosantangelo

Reputation: 13726

Try changing

$(changeItem).parent('.cartSubPrice').text(resultSub); 

By

$(changeItem).siblings('.cartSubPrice').text(resultSub);

Because, if I look at the html right you're trying to get an element in the same hierarchy, so if you have:

<element></element>
<parent>
      <child></child>
</parent> 

jQuery("child").parent(); //<parent></parent> 
jQuery("parent").siblings(); //[<element></element>] 

Also, as 3nigma suggested if you want only the first element that matches the selector, you could use .closest()

Upvotes: 1

elrado
elrado

Reputation: 5282

Something like:

('.cartSubPrice').html(123); OR
('.cartSubPrice:eq(0)').html(123); OR

Upvotes: 1

Rafay
Rafay

Reputation: 31043

try

 $(changeItem).closest('.cartSubPrice').html(resultSub);      
 $(changeItem).val(resultTotal);

Upvotes: 1

Related Questions