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

271
Visualizações
Could I have two onclick events in this one element?

So I do know that it is possible to have two onclick events in one element. But I'm not sure how to do it exactly for my situation. Is there another way of doing this that I'm not trying? I'm trying to add the ShowAlert() method.

My code looks like this:

<button class="btn btn-primary" @onclick="e => AddToCart(context)">Add To Cart</button>

And I have tried(I get an error every time I try these):

<button class="btn btn-primary" @onclick="e => AddToCart(context); ShowAlert();">Add To Cart</button>

and:

<button class="btn btn-primary" @onclick="ShowAlert(); e => AddToCart(context)">Add To Cart</button>

Here is my ShowAlert method in case it is needed:

@code
{
    async Task ShowAlert()
    {
        await JsRuntime.InvokeVoidAsync("createAlert");
    }
}

Any help/advice is appreciated! Thank you!

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

0

It looks like you're using some library I am unfamiliar with, so not sure if any of this is feasible.

  1. Do you need to attach these events inline on the element? The first thing my mind goes to is attaching them in JS

  2. If #1 is not possible, how about making a single joined function that calls both of them?

function addToCartAndShowAlert(context) {
   AddToCart(context)
   ShowAlert()
}
<button class="btn btn-primary" @onclick="e => addToCartAndShowAlert(context)">Add To Cart</button>

Just ideas to try, best of luck to you

about 4 years ago · Juan Pablo Isaza Relatório

0

The following should normally work, wrapping it in parentheses to call it like an anonymous function :

<button @onclick="(e => AddToCart(context))();ShowAlert();">Hi</button>

function AddToCart() {
  alert('add to cart');
}

function ShowAlert() {
  alert('show alert');
}
<button onclick="(e => AddToCart())();ShowAlert();">Hi</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this:

 <button class="btn btn-primary" @onclick="@(() => { AddToCart("content"); ShowAlert(); })">Add To Cart</button>
@code
{
    async Task ShowAlert()
    {
        await JsRuntime.InvokeVoidAsync("createAlert");
    }
    void AddToCart(string val)
    {
        StateHasChanged();
    }
}

result: enter image description here

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