Steps for displaying or hiding the mobile menu when clicked

I am struggling with displaying a menu and its mobile version in HTML. The mobile menu doesn't seem to be functioning properly. Is there a way to initially show only the menu SVG icon, hide the mobile menu, and then reveal the mobile menu along with the close SVG icon upon clicking the open menu button?

Currently, on the mobile version, the menu appears open by default with a button to close it, but the button does not hide the menu as intended.

You can check out an example here:

https://jsfiddle.net/2vjo0gnc/

HTML

<div class="relative bg-gray-50 overflow-hidden">
      <div class="max-w-7xl mx-auto">
        ... (omitted for brevity) ...
</html>

JS

const nav = document.querySelector('#mobile-nav');
const closeBtn = document.getElementById('close-mobile-nav-btn');

closeBtn.addEventListener('click', () => {
    console.log('Show/hide the menu');
});

Answer №1

To achieve this functionality, you must use Javascript. Create a variable called isOpen that determines whether something is open (true) or closed (false). Then, manage the navigation and button based on the value of this variable.

While Tailwind CSS does not include built-in Javascript capabilities, it includes useful HTML comments to guide you on how to implement opening and closing effects as desired.

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

Can you identify the date stored in this JSON object?

I need to analyze some JSON data This is the specific snippet required for my query. "UpdatedDate":"\/Date(1311377875937)\/" I'm unsure about that number, but the updated date indicates when the item was last modified Moreover, how can I ...

Utilize the <select> tag to dynamically update the state based on user input. Filter through a list of categories to selectively display blog posts from

I have created a dynamic dropdown list of categories by saving their names in an array and now I want to update my list of blog posts based on the selected category from the dropdown. The array containing categories is as follows: const tags = ['Sust ...

What is the suitable condition necessary for optimal functionality?

Greetings all! I am a newbie in the world of development and also new to Stack Overflow. This is my maiden post seeking assistance on the following issue: $(document).ready(function() { $(".header").click(function() { if ($(".header-content"). ...

Using jQuery to populate tables

Here is a table I am working with: <table class="table table-hover"> <thead> <tr> <th>Person</th> <th>Ethereum Address</th> ...

What is the proper method for sending a value using the XMLHTTPRequest Object?

In my table, I have a set of dynamically generated links. Each row in the table has a unique "id" property in its tag. The main objective is to use XMLHTTPRequest to inform the 'deletepost.php' page which specific record needs to be removed from ...

Ensure that the left div spans 100% of the width of the container, while the right div remains on the right side and maintains a

Is there a way to create an effect with a div where the left side is 100% and the right side stays fixed? <!DOCTYPE html> <html> <style> .left { width: 100%; float: left; border: #ccc solid 1px; } .right { width: 50px; float:left; border ...

"Implementing a jQuery dialog box that sends JSON response to a different page rather than the current

Currently, I am working on an MVC application where I need to insert properties of certain objects. To achieve this, I have created a modal popup using jQuery dialog. In order to avoid any interference with other user actions, I have implemented an Ajax.Be ...

Modify content or display picture within accordion panel based on its identifier

In my view, I have a model representing a list of items. Each item has a unique ID, which is included in the H2 header tag. The details of each item are displayed in a div below the header. Initially, there is an image within the header that is set to disp ...

Bind the prototype to the JavaScript object

Consider this scenario where I have JSON data containing person objects. [ { "name": "Alice", "age": 28, }, { "name": "Bob", "age": 33 } ] Upon parsing, an array with two JavaScript objects is obtained. Suppose I want to add a ...

Sort your list efficiently with a custom hook in React using Typescript

I've been working on developing a custom hook in React that sorts an array based on two arguments: the list itself and a string representing the key to sort by. Despite trying various approaches, I haven't been able to find a solution yet. I&apos ...

What is the best way to align these Iframes in the center?

This is the html <div class="main_content"> <section class="episodio"> <article class="contenedor_episodios"> <h2>Episodios</h2> <div class="episodio_spotify" ...

The CustomValidator ClientValidationFunction will only activate on the second or subsequent submission if CheckBoxList is checked

I am encountering an issue with a user control that is dynamically added to pages on DNN. The user control is built on a customized version of CheckBoxList and includes a CustomValidator with ClientValidationFunction set to a javascript function. It works ...

Tips on swapping elements in array with elements from a different array

Looking for a more efficient method to replace elements starting from the 0th element in one array with elements from another array of variable length. For example: var arr = new Array(10), anotherArr = [1, 2, 3], result; result = anotherArr.concat(arr); ...

What is the best way to ensure a stable background image using CSS?

I have successfully created my navigation section and now I am working on the main body of the website. However, when I try to add a background image, it ends up appearing on the navbar as well. Can someone please assist me with this issue? Below is the HT ...

Transform a protractor screenshot into a PDF file

I'm currently working on a small Protractor code that captures screenshots, but my goal is to save these screenshots as PDF files. Below you can find the code snippet I have written. await browser.get(url); const img = await browser.takeScreenshot(); ...

Press here to expose the remaining portion of the text using the truncate helper

Has anyone tried using jQuery with the truncate helper method in Rails? I've managed to turn the omission symbol into a link_to like this: <%= raw(truncate(@book.description, :length => 1000, :omission => (link_to' (continued)', :i ...

completed() versus finished()

As I was going through the documentation for passport, I came across an interesting observation regarding the use of serialize() and deserialize(). It seems that done() is called without being explicitly returned in one scenario. However, while setting up ...

Challenge with the Nested List Weight Sum algorithm

Trying to crack the challenge "Nested List Weight Sum": Challenge: If given the list [[1,1],2,[1,1]], the expected output is 10. (four 1's at depth 2, one 2 at depth 1) Here's my approach. function calculateDepthSum(nestedList, sum=0, dept ...

Make Angular able to open a new window using window.open instead of opening a popup

Can anyone help me figure out how to use window.open to open a PDF file in a new tab for user download? Below is the Angular 4 code I'm currently using: download() { const data = {html: this.template.toArray()[0].nativeElement.innerHTML}; th ...

Attempting to steer clear of utilizing $watch

In the following code snippet, a DOM element is modified if its width is less than 700px. I'm curious, is there an alternative way to achieve the same result without utilizing a watcher? const checkCalendarWidth = () => { if ($('#calenda ...