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

128
Visualizações
how can i set custom attribute with @ sign in javascript

i cant set a custom attribute for my button. Here is my code please check it, thanks.

btn.setAttribute('@click.prevent', 'login()')
<button name="user-auth-submited" id="user-auth-submit" class="user-auth-submit-btn" type="submit">ثبت نام</button>

I'm using alpine.js and I should have to use the @ and I want to toggle attribute @click.prevent', 'login() to @click.prevent', 'register()

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

0

You cannot set dynamic attributes like this, because it happens after Alpine.js initialization, therefore Alpine.js does not know about it. But there's a different method, in which we use a state variable, where we track whether the user wants to login or register.

<div x-data="{login: false}">
    <button @click="login = !login">Switch login/register mode</button></span>
    <button @click.prevent="login_or_register" x-text="login ? 'Login' : 'Register'"></button>

    <script>
        function login_or_register() {
            if (this.login) {
                // Login stuff
            }
            else {
                // Register stuff
            }
        }
    </script>
</div>

The first button toggles our new state named login. The second button calls the function login_or_register in which we access this.login variable and execute the selected login or register code. You can easily generalize it to more states if needed as well.

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