Changing button span via jQuery

1

I have a button that in the click of the same directs an ajax request to an external file that adds a product, however that does not matter much in this case. I wanted to manipulate what is written in the span of button. Every time the user clicked buy, it would change to remove. And, the reverse would also happen, every time the user clicks the remove button, the span of that button would change to buy. What I was able to do was that the span of the button clicked changed to remove, but the reverse did not yet.

Button code:

 <button type="button" class="button btn-cart" onclick="addCartao('<?php echo $_product->getId(); ?>')" name="cartaoMensagem<?php echo $_product->getId(); ?>" id="cartaoMensagem"><span><span>Comprar</span></span></button>

Ajax request code:

function addCartao(product_id) {
  $j.ajax({
    type: "POST",
    url: "teste.php",
    data: {
      product_id: product_id
    },
    dataType: 'json',
    cache: false,
    beforeSend: function() {

    },
    success: function(retorno) {
      $j('button[name=cartaoMensagem' + product_id + ']').html('Remover'); //Código que consegui fazer

    },
    complete: function() {

    },
    error: function(x, y, z) {
      alert("error");
      alert(x);
      alert(y);
      alert(z);
    }
  });
}
    
asked by anonymous 30.08.2017 / 21:32

3 answers

1

When using html all content is overwritten, so you must re-set span´s .

success: function(retorno) {
      var button = $j('button[name=cartaoMensagem' + product_id + ']');

      if($j(button).text() == '<span><span>Comprar</span></span>'){
              $j(button).html('<span><span>Remover</span></span>');
      }else{
              $j(button).html('<span><span>Comprar</span></span>');
      }


    },
    
30.08.2017 / 21:43
2

A more elegant and short way to do this:

success: function(retorno) {
  i = $j("#cartaoMensagem"+product_id);
  i.text() == "Comprar" ?
  ( i.html(i.html().replace("Comprar","Remover")), $j("#cartao b*texto grifado*utton").not(i).attr('disabled','disabled') )
  :
  ( i.html(i.html().replace("Remover","Comprar")), $j("#cartao button").removeAttr('disabled') );
},

Test below:

var $j = jQuery.noConflict();
function addCartao(product_id){
	i = $j("#cartaoMensagem"+product_id);
	i.text() == "Comprar" ? ( i.html(i.html().replace("Comprar","Remover")), $j("#cartao button").not(i).attr('disabled','disabled') ) : ( i.html(i.html().replace("Remover","Comprar")), $j("#cartao button").removeAttr('disabled') );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="cartao">
<button type="button" class="button btn-cart" onclick="addCartao(1)" id="cartaoMensagem1"><span><span>Comprar</span></span></button>
<br>
<button type="button" class="button btn-cart" onclick="addCartao(2)" id="cartaoMensagem2"><span><span>Comprar</span></span></button>
<br>
<button type="button" class="button btn-cart" onclick="addCartao(3)" id="cartaoMensagem3"><span><span>Comprar</span></span></button>
</div>
    
30.08.2017 / 22:28
1

$('span').on('click', function(){
   if($(this).attr('data-value') == 1){
      $(this).attr('data-value', 2)
      $(this).text('Remover')
   }else{
      $(this).attr('data-value', 1)
      $(this).text('Comprar')
   }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><spandata-value="1">Comprar</span>

There are several ways to do this, in which case I worked with attr and checked the value, changed the text and its value with each click.

    
30.08.2017 / 21:38