When I click on the anchor element, I wish for the navigation to be concealed

I am facing an issue with my navigation bar. It is functioning properly, but I am unable to activate the :active pseudo-class for the anchor tags when clicked. Here is my HTML and CSS code along with what I am trying to achieve...

Below is the HTML section of my navigation:

<div class="navigation">
      <input type="checkbox" class="navigation__checkbox" id="navi-toggle">

      <label for="navi-toggle" class="navigation__button">
        <span class="navigation__icon">&nbsp;</span>
      </label>

      <div id="background" class="navigation__background">&nbsp;</div>

      <nav class="navigation__nav">
        <ul class="navigation__list">
          <li class="navigation__item"><a href="#" class="navigation__link">About Me</a></li>
          <li class="navigation__item"><a href="#" class="navigation__link">Skills</a></li>
          <li class="navigation__item"><a href="#projects" class="navigation__link">Projects</a></li>
          <li class="navigation__item"><a href="#" class="navigation__link">Contact Me</a></li>
          <li class="navigation__item"><a href="#" class="navigation__link">Message Me</a></li>
        </ul>
      </nav>

    </div>

On clicking the hamburger button, the navigation expands. Clicking it again collapses the navigation. This behavior can be seen in the following SASS/CSS snippet:

.navigation {
  .navigation__checkbox {
    display: none;
  }

  // More CSS properties...

}

My objective is to replicate this functionality for the anchor tags within the navigation menu. I attempted to implement a similar approach without success. Any suggestions on how to achieve this would be greatly appreciated. Thank you.

If interested, please see the webpage for a better understanding of the issue at hand.

Answer №1

Utilize pure vanilla javascript for this task

1) Locate and gather all menu links on the page

const menuLinks = document.querySelectorAll('.navigation__link');

2) Attach a click event listener to each link, and update checkbox state to unchecked upon click

menuLinks.forEach(link => { 
   link.addEventListener("click", () => { 
     document.querySelector('.navigation__checkbox').checked = false
   })
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the text inside a div based on the navigation of another div

Hey there, experts! I must confess that I am an absolute beginner when it comes to JavaScript, JQuery, AJAX, and all the technical jargon. Despite my best efforts, I'm struggling to grasp the information I've found so far. What I really need is ...

Displaying images above a Bootstrap Carousel

I am looking to create a dynamic mobile image slider using Bootstrap carousel with overlaying images. However, I am facing two issues with responsive resizing and the overlapping of sections due to absolute positioning. <section> <h2 style= ...

Move logo and text when menu is clicked

I have been attempting to create a sliding animation on both the logo and header text when clicking on the menu button. The desired effect is for both elements to slide left, with the menu sliding out of the screen and the text sliding in to replace the lo ...

Assign a class to a button created dynamically using Angular

While working on my project, I encountered an issue where the CSS style was not being applied to a button that I created and assigned a class to in the component.ts file. Specifically, the font color of the button was not changing as expected. Here is the ...

Is there a way for me to have a background image behave like an img element with img-fluid class as shown in the

Showing 2 rows The issue lies with the first row, as it is not displaying properly and does not resize responsively like the second row. I have tried everything but nothing seems to work. I am attempting to do this because the text and elements in the ...

What is the best way to find the correct Xpath for the <li> tag in HTML?

In order to make Selenium click on the element of a drop-down menu using Java and IntelliJ, I need to click on the "today" button. Despite trying various methods like copying the xpath or using cssselector, including extensions like xpath finder, I have ...

CSS :hover activates only when the mouse is in motion

Looking at a simple example I put together: HTML <div id="sample"></div> CSS #sample { width:400px; height:400px; background-color:green; display:none; } #sample:hover{ background-color:red; } It's a hidden DIV tha ...

How can I incorporate a personalized SVG path to serve as a cursor on a webpage?

Is there a way to enhance the functionality of binding the 'mousemove' event to a div and moving it around the page while hiding the real cursor? Specifically, can we change the shape of the circle to an SVG path and drag the SVG path around the ...

React components may overlap each other due to their elements intersecting

I am currently facing an issue in my React project where the footer is overlapping with the elements at the bottom of the page. To view the live project, you can visit: Although I have researched similar questions on this topic and tried adjusting margin ...

The responsiveness of cards within the carousel-inner needs improvement

Despite successfully creating a carousel with 12 cards and 6 cards on each side, I am facing challenges with the responsiveness on mobile screens. I have tried numerous methods to make it responsive but have not achieved the desired results. The behavior o ...

What is the best way to create a hyperlink to the same page but on a separate website?

Currently, I am working on a test version of my website. I would like to add a link in the header of the test server that will direct users to the same page on the live server. Is there any way in HTML or PHP to determine what the current page is? ...

RegEx for capturing targeted content within HTML elements

I am in need of developing a Python program that takes an HTML file from the standard input, then outputs the names of species listed under Mammals to the standard output line by line utilizing regular expressions. Additionally, I am instructed not to incl ...

"Internet Explorer naturally selects the submit button when users press the enter key to submit a

In my current application, I have implemented a form with a hidden button to address issues with the numeric keyboard on Android. Essentially, pressing enter or focusing on the invisible button will trigger form submission. Pressing enter works fine in Ch ...

How can I implement custom color values for individual sides of the border property in tailwind-css?

I am attempting to utilize tailwind to create a triangle shape. Here is the original code I found on the Triangle Generator. To assign colors to each side of the border, I am using the border-b-color syntax with pre-defined theme colors, which works well. ...

What is the best way to align the 'Typography' component in the center?

Attempting to center it using flexbox with justify-content did not yield the desired result. I also tried replacing the Typography component with a div tag, but still no success. import {AppBar,Zoom,Toolbar,Typography,CssBaseline,useScrollTrigger,Fab,ma ...

Unable to assign a basic HTML class to a Blazor component

Upon attempting to implement the following code in my .NET 5 Blazor project within a .razor file: <SignedLabel class="some-css-class" Price=123 Currency=Currency.Usd /> I encountered an issue where Blazor interpreted the class attribute as ...

Consolidate HTML project into a unified HTML file

In my current project, I am working with a static HTML report that is organized in the structure outlined below: 1.css 2.font 3.js 4.pages 5.attachments 6.index.html I would like to know how I can combine all these elements to create a unified 'repor ...

The `background-size` property in jQuery is not functioning as expected

I am facing the following issue: When I click on a div with absolute positioning, I want to animate its position, width, and height. In addition, I want to change the background-size using jQuery. However, the problem is that all CSS properties are updat ...

Creating a responsive container in Bootstrap that adjusts height dynamically, featuring a text input box positioned at the bottom

I am currently using Bootstrap 4 to design a desktop chatbot inspired by Google's Bard. My goal is to have a container that spans the height of the screen, with the text input located at the bottom of the container. Additionally, I want the container ...

Setting the margin to auto causes the div to be shifted out of the

I'm facing an issue with my div element. The styling properties margin-left:auto and margin-right:auto are pushing another div out of the navbar. How can I keep both div elements within the navbar? Here is my code: body, html { margin:0; padding:0; ...