I have a div with two spans inside, I am checking to see if a text already exists inside inside that div, if it does then update the text inside of a span.

Here's the structure of my div:

<div class="top-cart-items">

<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Orange Club
<span class="top-cart-item-price">£2.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>

<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Blue Tang
<span class="top-cart-item-price">£2.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>

<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Soft Ball
<span class="top-cart-item-price">£3.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>

</div>

When a string is found inside .top-cart-item-desc I want to update the .top-cart-item-quantity using jQuery. There are a lot of divs and spans with the same names so I am unable to find where the string was found and update that.

This is what I have tried so far.

$('.add').click(function(){
    var name = $(this).attr("data-name");
    var price = parseFloat($(this).attr("data-price"));
    var quantity = 1;
        if($('.top-cart-item-desc').text().indexOf(name) != -1)
            {
            $(this).closest('.top-cart-item-desc span.top-cart-item-quantity').text('15');
            }
});

I have also tried siblings() and find() but none of those worked either.

Here's a fiddle

1 Answers

1
Mike Chatsky On Best Solutions

Try introducing a map variable to store new items added, then you can:

items[item].find('span.top-cart-item-quantity')

link: https://jsfiddle.net/3ajzmLnr/