I played around with it a little and was able to get it to work using the following code. There is probably a better way to do it so if there are any suggestions, it would be appreciated.
button
<button
x-data
itemid="{{$item->ItemID}}"
action="add"
x-on:click="addtocart($el);"
class="inline-block py-1.5 px-3 m-1 text-xs text-center text-white align-middle whitespace-nowrap bg-mediumBlue bg-none rounded border border-transparent">
Add
</button>
cart.js external file bundled through Vite
window.addtocart = function addtocart(e){
if(e.getAttribute('action') == 'add'){
var action = 'remove';
var actionText = 'Remove';
} else {
var action = 'add'
var actionText = 'Add'
}
e.setAttribute('action',action);
e.innerText = actionText;
axios.post('/cart/add', {
itemid: e.getAttribute('itemid'),
})
.then(function (response) {
//SweetAlert2
Swal.fire({
position: 'top-end',
icon: 'success',
title: response.data,
showConfirmButton: false,
timer: 1500
})
})
.catch(function (error) {
console.log(error);
});
}