JavaScript.

This code opens a navbar menu. How can I make it so that when I tap the same button it will be close? Please explain to me in a way as if you are explaining it to a beginner. Because I am a beginner 🙂 . If possible, show me a better way of doing this.
No description
30 Replies
Azyrum
Azyrum•12mo ago
https://jsfiddle.net/jphc39bf/1/ you can do something like this
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
FES001
FES001OP•12mo ago
It is not working.
Azyrum
Azyrum•12mo ago
provide a snippet of your code
FES001
FES001OP•12mo ago
Can we talk in a voice chat?
FES001
FES001OP•12mo ago
No description
FES001
FES001OP•12mo ago
No description
Jimster
Jimster•12mo ago
the code that azyrum provided shows that he used a class of "open" and when you click the button js toggles the class to the nav element
FES001
FES001OP•12mo ago
Should I put .links instead of open?
Azyrum
Azyrum•12mo ago
change the getElementsByClassName to querySelector(".links")
Jimster
Jimster•12mo ago
you can use whatever classname you want
FES001
FES001OP•12mo ago
I did
Jimster
Jimster•12mo ago
make sure you use querySelectorAll if more than 1 eleemnts have the links class
Azyrum
Azyrum•12mo ago
the .links is a div
FES001
FES001OP•12mo ago
No description
FES001
FES001OP•12mo ago
When I tap the button it should open .links menu and close it for the second time.
Jimster
Jimster•12mo ago
you should use links.classList.toggle(".visible") and css .links{ display: block; } .visible{ display: none; }
Azyrum
Azyrum•12mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
FES001
FES001OP•12mo ago
Where should I put the class .visible?
Azyrum
Azyrum•12mo ago
Here you have a nav wrapping the button and the links div. I store the ".links" div inside a variable. I add an eventlistener to the button, when i click the button it gives the links div a class
Jimster
Jimster•12mo ago
on the links variable
Azyrum
Azyrum•12mo ago
I'll comment inside the jsfiddle nvm
FES001
FES001OP•12mo ago
No description
Azyrum
Azyrum•12mo ago
man..
Jochem
Jochem•12mo ago
@FES001 it might be useful to provide a live example like suggested in #how-to-ask-good-questions, using codepen or jsfiddle like Azyrum is doing, so that everyone has a full view of what your problem is specifically
FES001
FES001OP•12mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
Azyrum
Azyrum•12mo ago
Ok so first There is no .visible in the css. If the class does not exist, nothing's going to happen
links.classList.toggle(".visible");

// should be

links.classList.toggle("visible");

links.classList.toggle(".visible");

// should be

links.classList.toggle("visible");

When you give a class via javascript, you only give the name of the class without the "."
FES001
FES001OP•12mo ago
So I did Sorry, as soon as I sent that link I went to have a lunch. Wow, it is working Thank you so much.
Azyrum
Azyrum•12mo ago
no prob
Want results from more Discord servers?
Add your server