VTek

VTek

Where do hooks go into the DOM?

1st project here. Trying to attach a hook. I’ve spent hours logging everything I can think of to figure out why I can see my hook mount and even verify it mounts to the id I gave the element, but nothing I do will fire it.

Is there a way to trigger it from F12 console to confirm it’s even there? I tried this, but get true, and no console.log that I’m expecting:

hookElement.dispatchEvent(new CustomEvent("phx:update"))

TL;DR but here’s the now obnoxious dysfunctional hook (and yes I do send it when I new LiveSocket):

console.log("Hook definition reached");
let Hooks = {};
Hooks.IntroductionComplete = {
	mounted() {
	  console.log("IntroductionComplete hook mounted on " + this.el.id);
	  this.lastEventTime = 0;
	  this.setupEvent();
	  if (this.el) {
		this.el.dispatchEvent(new CustomEvent("phx:update"));
	  }
	  else console.log("No element found");
	},
	updated() {
	  console.log("IntroductionComplete hook updated");
	  this.setupEvent();
	},
	setupEvent() {
	  console.log("Before setting up introduction_complete event listener");
	  if (this.hasEventListener) return;
	  setTimeout(() => {
		this.hasEventListener = true;
		console.log("Setting up introduction_complete event listener");
		
		this.handleEvent("introduction_complete", () => {
		  console.log("Entering introduction_complete event handler");
		  console.log("Introduction complete event received");
		  this.revealTextArea();
		});
		console.log("After setting up introduction_complete event listener");
	  }, 300); // Adjust the delay for testing
	},
	revealTextArea() {
	  console.log("Revealing text area");
	  // Implement logic to reveal or enable the text area here
	}
  };

Marked As Solved

LostKobrakai

LostKobrakai

That’s the incorrect part. You need socket = push_event(…).

Also Liked

regex.sh

regex.sh

You “dispatch” Hook by hooking it to an HTML element with id…

Adzz

Adzz

What are you trying to do with the hook first? revealTextArea implies showing / hiding something which can be done more simply either with CSS and HTML (eg a <detail> component) or with the JS functions like toggle.

If you just want to get a hook working start with something very simple that adds a class to change the color of the element clicked. That way you can proceed in steps and see if the problem is defining the hook, or what the hook does.

It doesn’t work, no matter where I put it. I call it like this and console logs tell me it’s called

This implies that the hook is “hooked up” correctly, suggesting that what you have written in the hook doesn’t do what you expect it to do.

Last Post!

Adzz

Adzz

The hook is just JS that gets added to the bundle. It is available on liveSocket.hooks. in the console but whether you can “trigger” it really depends on what the hook does. In general though I wouldn’t recommend that approach because the app cares about how it gets triggered by phoenix, so that’s what you should test and see in dev.

Live socket is defined here https://github.com/phoenixframework/phoenix_live_view/blob/main/assets/js/phoenix_live_view/live_socket.js you can enable things like enableDebug which may help depending on what you want

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement