how to create output event in JS click

Asked

Viewed 49 times

0

I am using this function for when an icon is clicked the menu disappear.

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 for the second time it comes back, but if I click again then the display does not 'None'. How to 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>
  • Have you tried using "block" instead of "grid"?

  • is that I’m using css grid layout

  • Post your html to get bigger help!

  • ready put

  • And if you switch a css class.. would no longer be practical ?

1 answer

1


It’s because you need to put one condition, otherwise there is no way to know the state of the element whether it is visible or not.

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>

  • It worked thanks! has how I get the grid properties by style? for example: "element.style.grid-Colum"

  • Yes, usually like any normal css property.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.