Alpine: @click is not a valid attribute name

Created on 20 Apr 2020  路  1Comment  路  Source: alpinejs/alpine

I would like to use Alpine.js in a Phoenix LiveView application for some very simple interactions like toggling a dropdown.

However in phoenix_live_view.js a setAttr() call is crashing because '@click' is not a valid attribute name.

How to reproduce

var div = document.createElement('div'); 
div.setAttribute("@click", "open = !open");
VM142:2 Uncaught DOMException: Failed to execute 'setAttribute' on 'Element': '@click' is not a valid attribute name.
    at <anonymous>:2:5

Most helpful comment

It's the norm in javascript, you would get the same error with any framework if you try to set an attribute containing the character '@' via code using setAttribute.
You can use the extended syntax 'x-on:click' and it should work. The @ syntax it's just a shortname to use directly in your html.

>All comments

It's the norm in javascript, you would get the same error with any framework if you try to set an attribute containing the character '@' via code using setAttribute.
You can use the extended syntax 'x-on:click' and it should work. The @ syntax it's just a shortname to use directly in your html.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allmarkedup picture allmarkedup  路  4Comments

bep picture bep  路  4Comments

adevade picture adevade  路  3Comments

dkuku picture dkuku  路  5Comments

haipham picture haipham  路  4Comments