My issue lies with the scratch on my JavaScript menu icon

Currently in the process of creating my portfolio website, but I've hit a roadblock with the icon menu - nothing happens when I click on it.

I attempted different solutions such as replacing the fontawesome icon with an image, seeking help from chatgpt (yeah, I know), and toggling between id and class attributes, but I'm feeling a bit confused at this point.

Here's the code snippet:

HTML

<header>
        <img src="assets/img/blanclogo.png" class="logo" alt="logo-blanc">
            <nav>
                <ul class="navlist" id="navlist">
                    <li><a href="#home" title="Accueil" class="active">Accueil</a></li>
                    <li><a href="#logiciel" title="Mes logiciels">Mes compétences</a></li>
                    <li><a href="#portfolio" title="Page liste portfolio">Portfolio</a></li>
                    <li><a href="assets/pdf/CV.pdf" title="CV">CV</a> </li>
                    <li><a href="#contact" title="formulaire">Me contacter</a></li>
                </ul>   
            </nav>
        <i class="fa-solid fa-bars" id="menu-burger"></i>  
</header>

Javascript


const header = document.querySelector('header');
const menuBurger = header.querySelector('#menu-burger');
const navList = header.querySelector('#navlist');

menuBurger.addEventListener('click', () => {
  menuBurger.classList.toggle('active');
  navList.classList.toggle('active');
});

navList.querySelectorAll('a').forEach((link) => {
  link.addEventListener('click', () => {
    menuBurger.classList.remove('active');
    navList.classList.remove('active');
  });
});

Answer №1

To change the menu icon to an X symbol, you can easily achieve this by modifying your code:

menuBurger.addEventListener('click', () => {
    if (menuBurger.classList.contains('fa-bars')) {
        menuBurger.classList.replace('fa-bars', 'fa-xmark');
    } else {
        menuBurger.classList.replace('fa-xmark', 'fa-bars');
    }
    navList.classList.toggle('active');
});

In the above code snippet, we first check if the icon class contains "fa-bars" using the function: contains("fa-bars"). If it returns true, we replace "fa-bars" with "fa-xmark" using:

menuBurger.classList.replace('fa-bars', 'fa-xmark');
. If it's false, we reverse the replacement operation:
menuBurger.classList.replace('fa-xmark', 'fa-bars');
.

Please let me know if you encounter any issues or need further assistance.

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

The initial value for the `useState` is not defined at the

Here's a simplified version of my requirement that demonstrates the issue. The ColorBox component receives a prop called "isVisible" from the ShowColorComponent component, which is used to initially set the state of the ColorBox.visible variable. impo ...

Using TypeScript, pass an image as a prop in a Styled Component

I am facing an issue with the code below that is supposed to display the "NoBillsLaptopPNG.src" image on the screen, but for some reason, the image is not showing up. The images are being imported correctly, so I'm unsure why the image is not appeari ...

Issue with IE11: the selected list is not displayed when using the <s:optiontransferselect> tag

When moving groups from left to right in the s:optiontransferselect for selectedGrps and unselectedGrps, the SelectedGroups list is showing as null on form submission in IE11. However, in Chrome and Mozilla, it functions correctly. Any advice would be grea ...

Is there a table that allows users to input percentages?

Looking for help with a table that has 2 columns: "Activities" and "Average % of Time". Users need to input a % value for each activity in the "Average % of Time" column. There are 7 rows for activities. The goal is to ensure that the sum of the % values f ...

Utilizing ag-grid with Vue.js: Implementing TypeScript to access parent grid methods within a renderer

I've integrated ag-grid into my project and added a custom cell renderer: https://www.ag-grid.com/javascript-grid-cell-rendering-components/#example-rendering-using-vuejs-components Although the renderer is working well, I'm facing an issue whe ...

Tips on preventing a lone track in Laravel for Server Sent Events

In my Laravel app, I am exploring the use of Server Sent Events. The issue I have encountered is that SSE requires specifying a single URL, like this: var evtSource = new EventSource("sse.php"); However, I want to send events from various parts/controlle ...

Can values from a dgrid store be saved in a variable or displayed in the console?

Currently, I am utilizing the dgrid store to display a grid (dgrid 0.4) on my website. Below is the code snippet that I have implemented: require([ 'dojo/_base/declare', 'dojo/Deferred', 'dstore/RequestMemory', ...

Issue Encountered: Problem with Implementing Google Fonts in WordPress Theme

I am currently facing an issue with a function in my Wordpress theme's functions file that is supposed to add Google Fonts to the theme. However, I keep receiving the following error message: Parse error: syntax error, unexpected '=', expec ...

What could be causing the issue of nth-child blocking the functionality of a:hover styles?

I have a unique collection of images that are styled to resemble polaroid pictures. Each image is given a slight rotation. a.polaroid { transform: rotate(-2deg); } On hover, a scale transformation is applied. a.polaroid:hover { transform: scale(1 ...

What is the best way to augment an AJAX array response with additional data?

I'm working on an AJAX request and I need to add additional properties to the response I receive. Can anyone offer some guidance? Here is the AJAX request code I'm using: var form_data = {}; $.ajax({ type: "GET", url: "../../../sample_ ...

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...

Ways to transfer JavaScript arguments to CSS style

Currently, I am developing a web service using Angular and Spring Boot. In this project, I have implemented cdkDrag functionality to allow users to place images in specific locations within the workspace. My goal is to maintain the placement of these image ...

How can I eliminate the hover effect from a div element?

I am facing an issue with implementing a zoom effect on hover for my list of products. When I do a long press on a product, it works the first time but not the second time. I suspect this is because the div remains in a hover state. I want to ensure that ...

Creating a JavaScript button that acts as a hyperlink

I've been trying to create a button that will redirect to a new link when clicked, but I've tried multiple options and can't seem to get it to work. var button = document.createElement("button"); button.innerHTML = "$1.00"; divInnerElement ...

Attempting to arrange a line consisting of four individual cells

I am facing an issue while trying to create a row with 4 cells and I can't figure out why it's not working. The structure involves a parent element called row with 4 child elements. <div className='row'> <div ...

Converting PHP date format to JavaScript date format

I'm struggling to solve this problem $datetime = new Date().toLocaleString(); // returns current date in format 10/21/2021, 14:29:43 How can I generate the desired date format using JavaScript? The output should look like this: 2021-10-21 16:30:01 ...

The contents of the $localStroage array do not align with those of the $scope array

I am storing objects in a `$localStorage` array for persistence. I have a check function to see if an object is already present in the array before adding or removing it (using `splice` if present, and `push` if not). However, after refreshing my page, th ...

My website footer is falling short and not reaching the bottom of the page

I've encountered an issue where my footer is not extending all the way down the website, despite trying various solutions. Here's what I attempted: I used the following CSS properties: min-height: 100%; max-height: 100%; I included these prope ...

Creating code that adheres to the ECMAScript 5 standards

In my quest to create a JavaScript library that adheres to modern standards such as HTML5, CSS3, and ESv5, I find myself wanting to break away from the mainstream libraries like jQuery and MooTools. While these are great tools, I believe in the importance ...

Renovating code structure in JavaScript

As I develop a small application that interacts with a database, I have opted to use Promises instead of the callback pattern for simplicity in my code. Through my coding experience, I've noticed a recurring pattern in my logic and I'm seeking ad ...