Change information with JS-JQUERY

Asked

Viewed 37 times

0

I am trying to change a certain value within my html code, through JS-JEQUERY, but this not changing, my logic is wrong ?

Follows the code:

var valor = "texto";
$('.botao botao-comprar principal grande desativo with-popover').on('click', function () {
   
   $(this).data('data-content', valor);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="javascript:;" class="botao botao-comprar principal grande desativo with-popover" rel="popover" data-placement="left" data-trigger="hover" data-content="Selecione uma opção de atributo disponível." data-original-title="IMPORTANTE">
  <i class="icon-shopping-cart"></i> Comprar
</a>

  • Cara, what is the value of your variable texto (var valor = texto;)? Because if it is a text, as the name suggests, it makes no sense to use increment valor++;

  • corrected, I was doing some tests

  • Failed to add the points in the classes, you just put in the first: $('.botao .botao-comprar .principal .grande .desativo .with-popover')

1 answer

1

If I understand correctly, you’re trying to manipulate the attribute date button. Then in the case as you have already used the method data you don’t have to write inside it data-content, but yes only content, tbm to manipulate an HTML element does not need to put all existing classes in it, only the first one is enough, if the class is unique, otherwise put a specific type $('.boot-buying') or use a id $(#button):

var valor = "texto";
$('.botao').on('click', function () {
   
   $(this).data('content', valor);

   console.log('O data-content agora é: ' + $('a').data('content'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="javascript:;" class="botao botao-comprar principal grande desativo with-popover" rel="popover" data-placement="left" data-trigger="hover" data-content="Selecione uma opção de atributo disponível." data-original-title="IMPORTANTE">
  <i class="icon-shopping-cart"></i> Comprar
</a>

  • Only one correction: it is not good to put only the class .botao on the selector, because there may be others on the page. It’s good that the selector is more specific, like $(".botao-comprar"). This class seems to me to be more specific.

  • @Sam yes, indeed. Inattention indeed! Thank you man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.