1
I have some buttons that at the click of it directs to a request in ajax to an external file that adds a product, however this does not matter much in this case. I manipulate these buttons, being that every time the button with the span
buy, where at the click of the same, the span
is changed to remove and when the remove button is clicked the span
is changed again to buy. I need that every time the buy button is clicked, all of them are disabled for the click less the button that is with the span
remove, so that this being clicked, the span
be changed to buy and the other buttons can already be clicked again.
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>
Code of the Ajax request:
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) {
var button = $j('button[name=cartaoMensagem' + product_id + ']');
if($j(button).text() == '<span><span>Comprar</span></span>'){
$j('#cartao').find(':button').attr('disabled',true);
$j(button).html('<span>Remover</span>');
}else{
$j(button).html('<span>Comprar</span>');
}
},
complete: function () {
},
error: function (x,y,z) {
alert("error");
alert(x);
alert(y);
alert(z);
}
});
}
Note: The code I made disables all buttons that are inside the card div, which are those that are in question, but as they are disabled, at the click of the button that is with the span
remove, same does no kind of action.
In the question you say you have 1 button and then you have other buttons. You can explain better or edit the question?
– Sam
I’ve edited it correctly now
– Matheus Portela