/    Sign up×
Community /Pin to ProfileBookmark

Converting inline onclick to event listener?

I have just started learning about event listeners and how to use them. Creating a new event listener from scratch seems easy enough. The problem is that I want to add an event listener to a previous code that relied on the html onclick solution.
In this case it is a navigation sidebar that I want to open and close when pressing a menu icon. When looking at my current js file, I have absolutely no clue where to place the event listener or how to make it link up with the code I already wrote.
I would be very thankful if someone could help me explain it to me and give me that “aha!” moment I have been looking for. 😁

THE HTML
`<body>

<nav class=”nav-main”>
<div class=”btn-toggle-nav”></div>
<ul>
<li><a href=”#”>menu1</a></li>
<li><a href=”#”>menu2</a></li>
<li><a href=”#”>menu3</a></li>
<li><a href=”#”>menu4</a></li>
</ul>
</nav>

<aside class=”nav-sidebar”>
<ul>
<li><span>menu1</span></li>
<li><a href=”#”>menu2</a></li>
<li><a href=”#”>menu3</a></li>
<li><a href=”#”>menu4</a></li>
</ul>
</aside>

</body>`

THE JAVASCRIPT

`let toggleNavStatus = false;

let toggleNav = function() {
let getSidebar = document.querySelector(“.nav-sidebar”);
let getSidebarUl = document.querySelector(“.nav-sidebar ul”);
let getSidebarTtitle = document.querySelector(“.nav-sidebar span”);
let getSidebarLinks = document.querySelectorAll(“.nav-sidebar a”);

if (toggleNavStatus === false) {
getSidebarUl.style.visibility = “visible”;
getSidebar.style.width = “272px”;
getSidebarTtitle.style.opacity = “0.5”;

let arrayLength = getSidebarLinks.length;
for (let i = 0; i < arrayLength; i++) {
getSidebarLinks[i].style.opacity = “1”;
}

toggleNavStatus = true;

}
else if (toggleNavStatus === true) {
getSidebar.style.width = “50px”;
getSidebarTtitle.style.opacity = “0”;

let arrayLength = getSidebarLinks.length;
for (let i = 0; i < arrayLength; i++) {
getSidebarLinks[i].style.opacity = “0”;
}

getSidebarUl.style.visibility = “hidden”;

toggleNavStatus = false;

}
}`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@codyhillSep 24.2019 — @m87dk#1609029 You are overcomplicating yourself.

Have a navbar open and close it very easy and you should do it with CSS classes.

I have written here the code that you should use.

https://codepen.io/raul-rogojan/pen/GRKzBmZ

If you don't understand something ask and I will explain it to you.
Copy linkTweet thisAlerts:
@cootheadSep 24.2019 — Hi RaulRogojan,

this can be done without _JavaScript_. ;)

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;show and hide menu&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: normal 1em / 1.5em BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;
overflow-x: hidden;
}
h1 {
font-size: 1.25em;
color: #555;
text-align: center;
text-transform: uppercase;
letter-spacing: 2px;
}
#cb {
position: absolute;
left: -999em;
}

#cb ~ label {
display: block;
width: 7em;
padding: 0.25em 0;
border: 1px solid #999;
border-radius: 0.5em;
box-shadow: 0.25em 0.25em 0.25em rgba( 0, 0, 0, 0.4 );
background-image: linear-gradient( to bottom, #fff, #ccc );
text-align: center;
cursor: pointer;
}

#cb ~ label::before {
content: 'show ';
}

#cb:checked ~ label::before {
content: 'hide ';
}

#cb:checked ~ ul {
right: 0;
}

#cb ~ ul {
position: absolute;
right: -100%;
padding: 3em;
background-color: #fff;
border: 1px solid #999;
border-radius: 0.5em 0 0 0.5em;
box-shadow: inset 0 0 1em rgba( 0, 0, 0, 0.4 );
transition: 0.5s ease-in-out;
}
#cb ~ ul a {
display: block;
padding: 0.25em;
margin: 0.25em 0;
color: #000;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;show and hide menu&lt;/h1&gt;

&lt;input type="checkbox" id="cb"&gt;
&lt;label for="cb"&gt;menu&lt;/label&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


_coothead_
×

Success!

Help @m87dk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...