Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rubendn's avatar

AlpineJS with external function

I have the following button that is using AlpineJS:

<button
   x-data
   itemid="{{$item->ItemID}}"
   action="add" 
    x-on:click="addtocart" 
   class="inline-block py-1.5 px-3 m-1 text-xs text-center text-white align-middle whitespace-nowrap">
       Add
  </button>

and the following script tag:

<script>
function addtocart(e){
    action = e.target.getAttribute('action');
    itemid = e.target.getAttribute('itemid');
   
    if(action == 'add'){
        action = 'remove';
        actionText = 'Remove';
    } else {
        action = 'add'
        actionText = 'Add'
    }
    e.target.setAttribute('action',action);
    e.target.innerText = actionText;

    axios.post('/cart/add', {
        itemid: itemid,
        _token: '{{ csrf_token() }}',
    })
    .then(function (response) {
        //SweetAlert2
        Swal.fire({
        position: 'top-end',
        icon: 'success',
        title: response.data,
        showConfirmButton: false,
        timer: 1500
        })
    })
    .catch(function (error) {
        console.log(error);
    });
}
</script>

This works fine but I'm trying to extract the function out to it's own file. I've changed the first line of the function to:

window.addtocart = function addtocart(e){

The function is being triggered but I'm getting the following error:

Uncaught ReferenceError: action is not defined

I'm guessing that by moving the function to an external file, it is no longer getting any data. How can I modify the button or function so that I can get the data from the button and also change the button data from the function?

Thank you.

0 likes
2 replies
rubendn's avatar

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);
    });
}
webrobert's avatar
    <button
        x-data
        itemid="123"
        action="add me"
        class="capitalize" 
        @click="addtocart">
        Add
    </button>

this.$el

    let action = this.$el.getAttribute('action');
    this.$el.setAttribute('action', action);
    this.$el.innerText = action;

    axios.post('/cart/add', {
        itemid: this.$el.getAttribute('itemid')
    })
...

Please or to participate in this conversation.