Ways to Conceal a Menu Item on a Specific Page

I am currently working on building a webpage with NodeJS and Express, utilizing the ejs templating engine. In my navbar, I have a menu item labeled add. When a user clicks on this item, it should take them to another page where they can input some data. However, what I want is for the "add" menu item to be hidden on the next page it links to.

Below is the code I have implemented:

nav.ejs

<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="/">Studious</a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
  <ul class="navbar-nav">
    <li class="nav-item">
      <a id="ad" class="nav-link active" aria-current="page" href="/add">Add</a>
    </li>
  </ul>

  </div>
 </div>
</nav>   

index.js

$(document).ready(function(){

 $("#ad").click(function(){

      $(this).hide();
  
   });
});

server.js

const express = require('express');
const path = require('path');

const app = express();

const port = process.env.PORT || 3000;

app.set('views', path.join(__dirname, 'views'));
app.set('view engine','ejs');

app.use("/public", express.static('public')); 
app.use(express.static(path.join(__dirname,'public')));

app.get('/',(req,res) => {

    res.render('index');
});

app.get('/add',(req,res) => {

    res.render('add');
});

app.listen(port,() => console.log(`App is running at ${port}`));

If you have any suggestions or solutions on how I can achieve the desired outcome, please let me know. Thank you!

Answer №1

  1. To ensure that current URL is passed to EJS pages, follow these steps:
  2. In the nav.ejs page, under specific conditions, make sure to include the /add link.

Example:

In server.js

app.get('/',(req,res) => {
    res.render('index', {
       currentUrl: req.originalUrl
    });
});

app.get('/add',(req,res) => {
    res.render('add' {
       currentUrl: req.originalUrl
    });
});

In nav.ejs

<ul class="navbar-nav">
    <li class="nav-item">
        <%if (currentUrl != '/add') { %>
            <a id="ad" class="nav-link active" aria-current="page" href="/add">Add</a>
        <% } %>
    </li>    
</ul>

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

A web address shared in a post appears within the nested frame of an iframe

I'm encountering a somewhat confusing issue. I am attempting to submit a URL to an iframe located on the same page. Upon clicking submit, the iframe replicates the current page, but with the correct URL displayed in the duplicated iframe. It's ma ...

Replica of Spotify - Bottom Section - HTML and CSS Styling

I have been attempting to replicate the Spotify interface using only HTML and CSS. While I have successfully created the player section, I am struggling to center the player and incorporate the other two details as shown in the attached images. The 'G ...

Using include() function causes conflict with Bootstrap functionality in PHP

Here is the HTML code I'm trying to use with my index.php file, but I'm encountering an issue. <!-- nav bar --> <nav id="top"> <nav class="navbar navbar-xs navbar-custom"> <div class="container-fluid" > <di ...

The Power Duo: jQuery Form Plugin paired with jQuery Validate

Have you ever tried using the jQuery Form Plugin in combination with the jQuery Validate plugin? The issue I'm facing is that even when the required fields are empty, the form still submits via AJAX. Check out my code snippet below: // Making the ne ...

Adjustable Font Controls

Currently, I'm exploring the benefits of utilizing variable fonts for a school project. As part of this project, I am working on incorporating sliders to adjust the font attributes. Specifically, I have chosen to use the Gingham variable font for thi ...

Issues with Materializecss sidenav functionality

Struggling to implement the sidenav with Materializecss. Check out the: MATERIALIZECSS SIDENAV DEMO MY CODEPEN https://codepen.io/gregoryksanders/pen/RxoyqB <head> <!--Import Google Icon Font--> <link href="https://fonts.googleap ...

The include function in jQuery fails to function as expected

Since my website was growing and the index.html file became too long and difficult to manage, I made the decision to split each section into separate HTML files and use jQuery to include them. I followed the instructions mentioned here on how to include a ...

Is there a way to make a div stay fixed at the bottom of the screen while allowing the middle content to scroll?

I am seeking a way to create a sticky footer that remains visible and anchored at the bottom of the screen, while allowing the middle content to scroll as new content is added. The method I have implemented for achieving this effect is as follows: /* Re ...

Display the value of a shortened string

My Goal I aim to develop a method for determining the amount of a string visible before it gets cut off. Motivation In my project, there is a collection of items that can be chosen. A panel presents a concatenated string of the selected item names separa ...

I would appreciate your assistance in the modification of the input type from text to radio within the PHP code

I've been struggling to update the input type from text to a radio button for Gender selection in my Wordpress theme's user profile settings. I can't seem to get it right in the code and I'm not sure how to make the change. I tried usi ...

Using JavaScript to parse JSON and set the value of a DatePicker

I have a text input field in my .cshtml page which is a Date type field. <div class="form-group"> <label for="comments">ETA:</label> <input class="form-control text-box single-line" data-val="true" id="MilestoneETAEdit" name ...

Struggling to retrieve the image URL from a TypeScript file

I'm currently developing a basic app in Ionic that will include a feature to display a list of registered users along with their profile pictures. These profile images are stored in Firebase storage. Although I have successfully retrieved the URLs fo ...

Updating a conditional statement in jQuery

Understanding jQuery has always been a challenge for me. I find myself spending hours reading documentation every time I try to implement it, only to achieve very little progress. Here's the code I'm currently working on with live-editing availab ...

Access constantly updating content using Python Selenium

This question has come up before, and despite my efforts to search and troubleshoot, I'm still struggling to make it work. As a beginner in Selenium, I could use some guidance. Take a look at: I'm attempting to scrape the "Recommended Rating", ...

Encountering a red squiggly line while working on an Angular 2 project in an HTML file within Visual Studio

Whenever I incorporate Angular 2 code into my HTML files, I notice red squiggly lines appearing like this: https://i.sstatic.net/fhXGu.png Hovering over the red squiggly lines does not reveal any errors or warnings. It may be worth mentioning that I am ...

Tips for Printing a Page Using CSS and Bootstrap Formatting

I'm having trouble getting my website to print with its CSS and bootstrap styles. When I try to print using ctrl + p or the window.print() function, only the div, controls, and buttons appear without their corresponding colors. I've attempted to ...

"Utilizing jQuery to access nested JSON structures. I am looking to access nested arrays within a JSON

{ "data": [ [ { "Id": 2, "Name": "John Doe", "city": "New York", "location": "Times Square", "landmark": null, "pincode": 12345, ...

Convert the button element to an image

Can someone please explain how to dynamically change a button element into an image using javascript when it is clicked? For instance, changing a "Submit" button into an image of a check mark. ...

The test() function in JavaScript alters the output value

I created a simple form validation, and I encountered an issue where the test() method returns true when called initially and false upon subsequent calls without changing the input value. This pattern repeats with alternating true and false results. The H ...

Changing the height of a pseudo element according to the width of the viewport in a dynamic way

Currently, I am utilizing the :before pseudo-element on a 100% width div to create a curved top, which is functioning correctly. However, I am facing the issue of adjusting the height of the :before element through media queries whenever the browser width ...