trifesk
trifesk

Reputation: 55

Change element on web with jQuery

I am beginner web developer.

I have script:

function updateBaskket(productId, quantity) {
    $.ajax({
        type: 'GET',
        url: '/update-basket',
        dataType: "json",
        cache: false,
        data: {
            productId: productId,
            quantity: quantity
        },
        success: function (data) {
            $('.basket-summary-amount').html(data.summary + ',- zł');
            $('[data-item-price=productId]').val(123);
            console.log(data);
        }
    });
}

    $('.quantity').on('change', function () {
        if($(this).attr("data-id") != undefined && $(this).val() != undefined){
            updateBaskket($(this).attr("data-id"), $(this).val());
        }
    });

It's work fine. In result I have:

{"productId":"2","price":"1.35","totalProductPrice":"23.12","summary":"16604.67"}

The key is productId

I need to find at the moment on the site:

  1. data-item-price="2" => 5291.35 and change value from 5291.35 to 1.35 (price)

  2. data-item-totalprice="2" => 15874.05,- zł to 23.12 (totalProductPrice)

On the website I have different items (with different id) data-id / data-item-price / data-item-totalprice. I would only like to update those that have id = 2

How can I make it?

Please help me

Upvotes: 1

Views: 49

Answers (1)

RatajS
RatajS

Reputation: 1429

Try something like this:

$('[data-id="'+data.productId+'"]').data({ 'item-price': data.price, 'item-totalprice': data.totalProductPrice });

Upvotes: 1

Related Questions