Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!