Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simple parameter function doesn’t work as intended

So I got a simple script which allows me to show/hide my hamburger navigation.


document.getElementById("hamburger_menu").addEventListener("click", show);

    function show() {
        document.getElementById("navigation").classList.toggle("show");
    }

This script works perfectly fine. As I want to reuse the script to show my searchbar I decided to use parameters in my script. To make this happen I made my script like this:


    function show(target) {
        document.getElementById(target).classList.toggle("show");
    }

document.getElementById("hamburger_menu").addEventListener("click", show("navigation"));

But for some weird reason this script is completely broken. Any advice here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The event handler for your click event can’t be a function call, it needs to be an actual function.
You could do an anonymous function like

document.getElementById("hamburger_menu").addEventListener('click', () => show("navigation"));

instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading