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
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.
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.