how to create output event in click on JS

0

I'm using this function for when an icon is clicked the menu disappears.

let sidebar = document.querySelector('.sidebar-page')

let sidebar_toggle = document.querySelector('.sidebar-toggle-box')
sidebar_toggle.addEventListener('click', function(){
  sidebar.style.display = 'none'

  sidebar_toggle.addEventListener('click', function(){
    sidebar.style.display = 'grid'
  })
})

When I click the second time it comes back, but if I click again then the display will not be 'none'. How do you fix this?

html:

<body>
<section class="all-page-content">

  <header>

    <div class="sidebar-toggle-box">
      <i class="fas fa-bars"></i>
    </div>

    <div class="medium-column-header">

      <div class="search">
        <input type="search" name="search" value="" placeholder="Pesquisar"/>

        <button type="button" name="button"><i class="fas fa-search"></i></button>
      </div>
    </div>

    <div class="black-row"></div>

  </header>

  <aside class="sidebar-page">
    <nav>
      <ul>
        <li><a href="#"> <div class=""><i class="fas fa-cogs"></i><p>loremsd</p></div></a></li>
        <li><a href="#"> <div class=""><i class="fab fa-btc"></i><p>lorisd</p></div> </a></li>
        <li><a href="#"> <div class=""><i class="fas fa-bell"></i><p>losari</p></div> </a></li>
        <li><a href="#"> <div class=""><i class="fas fa-chart-line"></i><p>loresa</p></div> </a></li>
      </ul>
    </nav>
  </aside>

  <aside class="right-sidebar">
    <address class="">
      <ul>
        <li><a href="#"><i class="fab fa-instagram"></i></a></li>
        <li><a href="#"><i class="fab fa-twitch"></i></a></li>
        <li><a href="#"><i class="fab fa-youtube"></i></a></li>
        <li><a href="#"><i class="fab fa-whatsapp"></i></a></li>
      </ul>
    </address>
  </aside>

  <section class="content">
    <article class="">
      <div class="tittle">
       <h6>Lorem ipsum dolor sit amet, est laborum.</h6>
      </div>

      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

      <aside class="">
        <h6>Author: Giovani Lopes</h6>
        <data value="24/11/2022">24/11/1996</data>
      </aside>
    </article>

    <article class="">
      <div class="tittle">
        <h6>sunt in culpa qui officia deserunt mollit anim id est laborum.</h6>
      </div>

      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </article>
  </section>

  <footer>
    <div class="">
      <p>Copyright (c) 2018 Copyright Holder All Rights Reserved.</p>
    </div>
  </footer>

</section>
<script type="text/javascript" src="js/teste.js" ></script>
</body>
    
asked by anonymous 25.05.2018 / 18:58

1 answer

1

let sidebar = document.querySelector('.sidebar-page');
let sidebar_toggle = document.querySelector('.sidebar-toggle-box');

sidebar_toggle.addEventListener('click', function(){
	if(sidebar.style.display != 'none') {
		sidebar.style.display = 'none';
	} else {
		sidebar.style.display = 'grid';
	}		
})
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet"/>

<section class="all-page-content">

	<header>

		<div class="sidebar-toggle-box">
			<i class="fas fa-bars"></i>
		</div>

		<div class="medium-column-header">
                     <div class="search">
		         <input type="search" name="search" value="" placeholder="Pesquisar"/>
                         <button type="button" name="button"><i class="fas fa-search"></i></button>
	             </div>
	        </div>

		<div class="black-row"></div>

	</header>

		<aside class="sidebar-page">
			<nav>
				<ul>
					<li><a href="#"> <div class=""><i class="fas fa-cogs"></i><p>loremsd</p></div></a></li>
					<li><a href="#"> <div class=""><i class="fab fa-btc"></i><p>lorisd</p></div> </a></li>
					<li><a href="#"> <div class=""><i class="fas fa-bell"></i><p>losari</p></div> </a></li>
					<li><a href="#"> <div class=""><i class="fas fa-chart-line"></i><p>loresa</p></div> </a></li>
				</ul>
			</nav>
		</aside>

		<aside class="right-sidebar">
			<address class="">
				<ul>
					<li><a href="#"><i class="fab fa-instagram"></i></a></li>
					<li><a href="#"><i class="fab fa-twitch"></i></a></li>
					<li><a href="#"><i class="fab fa-youtube"></i></a></li>
					<li><a href="#"><i class="fab fa-whatsapp"></i></a></li>
				</ul>
			</address>
		</aside>

	</section>
    
25.05.2018 / 20:24