Danilo Ribeiro
Danilo Ribeiro

Reputation: 99

I can not access element with jquery

this is my code:

<div class="price-all mg-top-40px planolista">
                                   <span class="price1">3x<br>R$</span>
                                   <span class="price2">16,<span>63</span></span>
                                   <span class="avista">ou R$ 49,90 à vista</span>
                                   <select class="form-control form-plan variante" data-idplano="2">
                                                                 <option value="2">Light  3</option>
                                                                  <option value="3">Light  6</option>
                                                                  <option value="4">Light  9</option>                    
                                    </select>

and this is my js :

   $('body').on('change','.variante',function(){
   $.get("classes/acao.php", {mudarvalorplano: $(this).attr('data-idplano'), idvariavel: $(this).val()}, function(resposta){
      var retorno_reposta = resposta.split("###");

      $(this).parent('div').find('.price1').html('something');

   });

I can not change the content of the class price2

what should I do? Is there something wrong with this piece of code.

$(this).parent('div').find('.price1').html('something');

it is not working

=(

Upvotes: 0

Views: 40

Answers (4)

Anshul Bisht
Anshul Bisht

Reputation: 1654

You should replace html() with text()

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

Use a closure because inside $.get() success callback, this is referring to jqXHR object. See e.g:

$('body').on('change', '.variante', function () {
    var $price1 = $(this).closest('.price-all').find('.price1');
    $.get("classes/acao.php", {
        mudarvalorplano: $(this).data('idplano'),
        idvariavel: this.value
    }, function (resposta) {
        var retorno_reposta = resposta.split("###");    
        $price1.html('something');    
    });
});

Upvotes: 0

Jb17
Jb17

Reputation: 19

Only one Price1 class is there in your code. For that why do you need a traversing. Better use like this.

$('.price1').text('Something');

No need to use .html here I guess.

Upvotes: 1

Sam
Sam

Reputation: 33

have you tried it the jQuery way?

$(".price1").html("something");

Upvotes: 0

Related Questions