Switch between three different classes

I need help with the flyout menu on my website usaletsgo.de. Specifically, I am looking to add a third page to the existing two pages in the red flyout located at the upper left corner of the site.

Currently, when switching between page 1 and page 2, list elements toggle between the css classes .visible and .hidden. The CSS code for this functionality is:

.visible{
display:block;
}

.hidden{
display:none;
}

The JS and HTML code snippet used for toggling list elements between visibility states is as follows:

<script>
function switch(){
$('.toggle').toggleClass('visible hidden');
};
</script>

<li class="toggle visible">Factory Butte </a></li>
... (list of other li elements)
<li class="toggle visible"> <a href="javascript:switch()">Page 2</a></li>
<li class="toggle hidden"> <a href="javascript:switch()">Page 1</a></li>

While this code may not be very advanced, it serves its purpose effectively. As a Javascript beginner, I am trying to figure out how to incorporate a third page into the existing functionality. Any suggestions or guidance would be appreciated!

Answer №1

Check out this clever code snippet that showcases some neat tricks you can implement. One of the cool features is its ability to handle multiple menus seamlessly.

$(function() {
  var $uls = $("#menu ul"), len = $uls.length; 
  $("#menu a").on("click",function(e) {
    e.preventDefault(); // This link acts as a placeholder for cursor:pointer with a span element
    var vis = $(this).closest("ul").index()+1; // Identifying which UL the link belongs to
    if (vis>=len) vis=0;
    $uls.hide().eq(vis).show(); // Display the relevant UL
  }).eq(len-1).click(); // Click on the last link to show the first when loading
  $("#menu").show(); // Display the menu (prevents all elements from showing at once and then hiding)
});
#menu { display:none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="menu">
  <ul>
    <li>Factory Butte</li>
    <li>Moonscape Overlook</li>
    <li>Wedge Overlook</li>
    <li>Fantasy Canyon</li>
    <li>Hamburger Rocks</li>
    <li><a href="#">Page 2</a></li>
  </ul>
  <ul>
    <li>Leprechaun Canyon</li>
    <li>Canyon X</li>
    <li>Paria Canyon</li>
    <li>Gold Butte NM</li>
    <li>Vermilion Cliffs NM</li>
    <li><a href="#">Page 3</a></li>
  </ul>
  <ul>
    <li>Bla Canyon</li>
    <li>Canyon Bla</li>
    <li>Paria bla</li>
    <li>Gold bla NM</li>
    <li>Bla Cliffs NM</li>
    <li><a href="#">Page 1</a></li>
  </ul>
</div>

Answer №2

I appreciate all of your support.

Thanks to a friend's assistance, everything is now functioning smoothly.

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

Is there a way to retrieve data from Apollo-Server in nodeJs without relying on React?

Is there a way to fetch data from Apollo-Server (GraphQL) without using React? I tried using node-fetch based on some tutorials but it didn't work as expected. Here is the code snippet I used for fetching data: const getData = (query)=>{ return fe ...

I'm seeking assistance in enhancing this code within tb for extracting values using JavaScript

Currently, I am extracting values from a table using JavaScript. The code seems to be functioning correctly, but I am looking for ways to optimize it and ensure that it is compatible with most modern browsers. The list in the table undergoes frequent chang ...

Surprising automatic scrolling upon pressing the keydown button in Bootstrap

My webpage appears normal with the Bootstrap framework, but whenever I press the down key, the screen scrolls down and displays unexpected white space due to reaching the end of the background-image sized at 1798px * 1080px. Please refer to the image: htt ...

Attempting to develop a straightforward JavaScript presentation display

The slideshow will cycle through six images named 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, and 6.jpg var showarray = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg"]; var i = 0; for( i = 0; i < 6; i++) { // Is there a way to pause the script for two ...

Participating in a scheduled Discord Voice chat session

Currently, I am in the process of developing a bot that is designed to automatically join a voice chat at midnight and play a specific song. I have experimented with the following code snippet: // To make use of the discord.js module const Discord = requ ...

Trouble with triggering HTML5 FileReader() functions

I'm having trouble determining why neither readSuccess() nor readFailure() are being executed in the code snippet below: function readMyFile(){ var reader = new FileReader(); reader.onload = readSuccess; reader.onerror = readFailure; ...

Using Javascript to create a radio button group

Is there a way to trigger an alert message when all radio buttons are checked as 'no'? I currently am only able to check each radio button individually. //The method I currently know $('#attraction1').change( function(){ if ($(this ...

This code snippet, document.location.search.replace('?redirect=', '').replace('%2F', ''), is failing to execute properly in Firefox

The functionality of document location search replace redirect to another page works in Chrome, however, document.location.search.replace('?redirect=', '').replace('%2F', ''); it does not work in Firefox; instead, ...

Limiting the functionality of API's to be exclusively accessible within the confines of a web browser

Currently, I am working with node js and have implemented policies to restrict API access from sources other than the browser. In order to achieve this, I have included the following condition in my code: app.route('/students').all(policy.checkH ...

Issues with form submission and JavaScript functionality have been detected in Internet Explorer, Firefox, and Safari, but are functioning properly in Chrome

Hey there! I've encountered a puzzling issue with a form I've created. It's programmed to determine the next page to redirect to using JavaScript after submission. Oddly enough, everything functions perfectly when I test it out in my IDE (C ...

Automatically inserting a row into an HTML table

My table structure looks like this: <form name="frm_data_nasabah2" method="post" enctype="application/x-www-form-urlencoded" action="<?php echo $page_action;?>"> <table class="table table-bordered table-hover" id="tab_logic_ ...

How do I retrieve a nested object element based on the value of a specific field in Mongoose?

Below is the teamModelSchema schema that I am working with. var teamMemberModelSchema = new mongoose.Schema({ "email": { "type": String, "required": true, "min": 5, "max": 20 }, "name": { "type": String ...

Easily transform arrays into objects by dynamically changing the keys

I have a comma-separated list that looks like this: var arr = [1,2,3,4] and I want to transform it into the following format: var new_arr = [ {x:1, y:2}, {x:3, y:4} ] I'm having trouble figuring out how to achieve this key/value transformation. ...

Accessing several values in Angular by clicking

I have a dropdown that contains an input field and two more dropdowns. I am looking for a way to retrieve the values of all three fields when the search button is clicked. Screenshot https://i.sstatic.net/5Imaq.png Code HTML <nz-dropdown-menu #menu=&q ...

Ways to ensure that text wraps to a new line when it exceeds the container's width

I am currently working on implementing a line of text within an ion-card element, but the challenge lies in the fact that the length of the text varies each time. In order to ensure that the entire text is visible and not cut off, especially on devices wit ...

How can a Windows service interact with an Electron application?

Currently, I have a Windows Service set up to regularly check if my application is running. If the application is running, the service then checks for any updates on a server. In the event of an update being available, I am looking for a way for the servic ...

Converting JSON object to a string

I have an object that contains the value "error" that I need to extract. [{"name":"Whats up","error":"Your name required!"}] The inspector displays the object in this format: [Object] 0: Object error: "Your name required!" name ...

Is it possible to convert the pixel Array from canvas.getImageData() into base64 encoding?

After using a base 64 encoder on my canvas.getImageData() pixel array, I am trying to save the image to a file using its base64 encoded string. Unfortunately, I cannot use canvas.toDataURL because it is not supported in webOS. Below is the code I have wr ...

display and conceal elements and refresh action

Can anyone help me with a function to hide and show a div? function toggledDivVisibility(divName) { if (divName.is(':hidden')) { var hiddenDiv = document.getElementById("filter"); hiddenDiv.style.display = 'block&a ...

What is the best method for selecting the parent using jQuery?

How can I dynamically add a "selected" class to the parent item if any of its children have a "selected" class in my recursive menu setup shown below? <ul> <li class="administration first"> <a href="/administration.aspx">&l ...