How to get another div with the same name without changing the others?

0

Well it's as follows. I have the following code structure:

A div called part-sec that brings the entire contents of the product. The price-product div comes with the price. So far so good. But below I have another div with the sizes and when the person clicks on the size he has to change the price-product div with the price of the size that the person clicked on. But the problem is that it has several divs equal to this and if I click it it changes the price of the size clicked on all the divs. I wanted it to only change the price of the product with the div clicked. I've tried this and it did not work!

small medium great

    
asked by anonymous 13.07.2017 / 17:11

2 answers

1

Felipe talks,

You are giving the order to change all the divs with the "pre-product" class. To resolve your issue, you can do the following:

$(document).on("click",".tam-selecionado",function(e) { 
    e.preventDefault();
    //separamos o elemento do click
    var elemento = $(this);
    .
    .
    .
    .

    elemento.closest('.part-info').find('.precoproduto').html(precoproduto);

We basically separate the click element; through it we look for the closest parent div with the 'part-info' class; we go to the 'pre-product' class and change the value

    
13.07.2017 / 21:08
0

Whenyouhoverovertheitemtype(small,medium,large),selectthelargeone;anditchangesintheX-SALADandintheX-EVERYprice,thatis,thepricestaysthesame.HoweverheshouldonlychangethepriceonX-SALAD,notonX-ALL.Hereisthecode:

$(document).on("click", "tam-selected", function (e)       {         e.preventDefault ();

    var tamanhoSelecionado = $(this).text();
    var idcategoria = $(this).attr("data-id");
    var idproduto =  $(this).attr("data-produto-id");
    var precoproduto = "";

    $.ajax({
      url: 'http://teste/store/produtos?filter[categoria_id]=' + idcategoria,
      method: 'GET',
      success: function(retorno)
      {

        retorno.data.forEach(function(item)
        {
          var idprodutoselecionado = item.produto_id;

          if (idprodutoselecionado == idproduto)
          {

            item.tamanho.forEach(function(tamanho)
            {

              tamanhoProdutoArray = tamanho.tamanho.nome;

              if (tamanhoProdutoArray == tamanhoSelecionado)
              {
                precoproduto = tamanho.valor;
                **$(".precoproduto class-id" + idproduto  + "").empty();
                $(".precoproduto").html(precoproduto);**
              }

            })
          }

        })
      },
      error: function(XMLHttpRequest, textStatus, errorThrown)
      {
        alert("Status do Servidor: " + textStatus);
        alert("Erro do Servidor: " + errorThrown);
      }

    });
  });
});
    
13.07.2017 / 18:38