Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda