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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
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

We're in Beta

About us Mission Statement