marius95

marius95

Javascript Event Handler is firing twice

Hello everyone,

I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file:

const mainAccordion = document.getElementById("main-accordion");
mainAccordion.addEventListener("click", () => {
    const navigationWrapper = document.getElementById("navigation-wrapper");
    navigationWrapper.classList.toggle("nav-active")
});

Apparently when I put a log message inside the function I see that the function is fired twice in a row.

How to ensure that the function is only fired once?

Edit: Sorry for debugging purposes I change the method from toggle to add. It’s now changed back how it should be

Marked As Solved

derek-zhou

derek-zhou

I always use a named function for even handlers. With a anonymous function if you run the code twice you get two identical handlers.

Last Post!

marius95

marius95

Ty for the ressource. I use a named function before but that didn’t fix the issue.

I had the same result with:

const toggleAccordion = () => {
    const navigationWrapper = document.getElementById("navigation-wrapper");
    console.log(navigationWrapper)
    navigationWrapper.classList.toggle("nav-active")
}

So I played around and read your article and I tried the following solution:


const toggleAccordion = (event) => {
    event.preventDefault()
    const navigationWrapper = document.getElementById("navigation-wrapper");
    console.log(navigationWrapper)
    navigationWrapper.classList.toggle("nav-active")
}

The event is triggered twice WITHOUT the event.preventDefault(). I don’t know why but it works.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement