Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
AlpineJS on-click executed twice

i'm new to AlpineJS and while learning it and implementing into my project, i found something bizarre that i can't resolve on my own. I have a Blade template that looks like this:

<form action="<some_url>" method="POST" style="display: contents">
@csrf
<button tabindex="-1" type="submit" class="focus:animate-ping w-full md:w-1/2 block hover:bg-gray-50 rounded-2xl">
    <img class="object-cover w-full h-full rounded-lg max-h-64 sm:max-h-96" src={{ <some_url> }}>
</button>

{{--img below is causing issues!--}}
<img x-data="{
        id: '{{ <some_hash_id> }}',
        _token: <csrf-token>
    }"
    @click="fetch('/bookmark', {method: 'POST',body: JSON.stringify(id)}).then(response => {
        if (response.ok)
            alert('Test - ok')
        else
            alert(`Test - problem`)
        })" 
    class="m-4 self-start" src="<some_img_url>">
    <input type="hidden" name="token2" value="{{ <another_token> }}">
</form>

The problem is that when i click on my img i get two alerts (both are the same, it's not a problem as on backend i haven't yet implemented an endpoint). I tried looking at AlpineJS docs, i tried using .stop and .prevent, i tried even without form. And result was always the same :(

All the time npm run watch is running and i'm always reloading the page with "empty cache" to make sure cache won't mess with it.

What might be a problem here? Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try wrapping it in a function

<img x-data @click="() => { fetch('https://icanhazdadjoke.com/', {method: 'GET'}).then(response => {
        if (response.ok)
            alert('Test - ok')
        else
            alert(`Test - problem`)
        }) }" src="https://cdnjs.com/_/f7a2ebfb819c118086546e481876aef6.svg" />
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda