dojap

dojap

How can I use JavaScript in Phoenix LiveView?

I am having troubles with running JavaScript inside Phoenix.

Things that work in a normal html file doesn’t seem to work from Phoenix LiveView templates.

I need some basic stuff to work and can’t find any tutorial or docs.

Thanks for any links.

Most Liked

tomkonidas

tomkonidas

What you are looking for is Javascript Interop in the Phoenix LiveView docs.

edisonywh

edisonywh

That is not how Phoenix LiveView work, there’s a couple of great resources out there explaining the programming model (Chris has a great video on it, though I can’t remember which conference it was).

The rough outline is that, when a user visits a LiveView page, Phoenix sends down a bunch of HTML down the server, the client then connects to LiveView via WebSocket (powered by Phoenix Channels). Then, given that you have attributes like phx-click, these events are then sent to LiveView via websocket, and LiveView responds via websocket - as you can see, nothing is done “fully on browser” as you initially thought.

This is where the JS interops come in though, for example say things like a menu state where you don’t need to send things to backend, you can do it fully locally like you want with JS interops. There’s two ways to do this, either via LiveView’s hooks, or via external dependency like Alpine.

tomkonidas

tomkonidas

So what you would do is set up a hook in your JS that your button will trigger, and then in the Hook (JS) you could access your cookie, do whatever you want, and push the result back to the LiveView to be handled.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement