dev234

dev234

Send JavaScript variables from client to server

Hi I have the same issue that this person had here, but I do not think the solution is really relevant to what I am looking for and I don’t understand it that well without context. I tried to ask for clarification there, but I was not able to receive a response.

I just simply need a way to pass a variable from the client-side JS to the server-rendered views/templates.

I did look into this, but that honestly just appears to be above and beyond what I need and I’m wondering if there is a simpler way.

I am using Phoenix LiveView with Surface UI. I am new to elixir/phoenix. Thank you for any help.

Marked As Solved

trisolaran

trisolaran

Add a js hook, and put the logic in the mounted callback of the hook. From there you get the viewport information and push it to the server. mounted callback is executed on the client after the LV has completed mounting.

Also Liked

mpope

mpope

It depends on how complex your use-case is. If you need the Javascript variable to go through any logic on the server, then checkout LiveView Javascript Hooks, which work very well and you can create GenServer-like LiveControllers that react to client events. Most of the boilerplate is already setup in the included LiveView javascript.

However, if you simply want to display the values then the server can render an empty div with a specific id (tag) and the client-side JS can then use getElemementByTagName and set the value setting the innerHTML on the returned element.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_event/3

This is the function that exists for pushing a message to JS.

trisolaran

trisolaran

this should be handle_event("test_event", %{"data" => your_data}, socket)

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you elaborate about why hooks are a poor fit?

dev234

dev234

That’s what I figured, thank you again everyone for all of your help

Where Next?

Popular in Questions Top

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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement