Using a jQuery script, you can enable a back button functionality that allows users

I created a survey to assist individuals in determining where to go based on the type of ticket they have. However, I am currently facing difficulties with implementing a "Back" button that will display the previous screen or "ul" that the user saw. Initially, my plan was to remove the 'active' class from the current "ul", but that approach did not work. Can someone please guide me on how to implement the back button effectively?

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
    <script src="js.js"></script>

  </head>
  <body>
    <section class="question_wrapper">

      <ul class="tab active">
        <li class="active">
          <p>Do you possess a ticket?</p>
          <a href="#tab1">yes</a>
          <a href="#tab2">no</a>
        </li>
      </ul>

      <ul class="tab" id="tab1">
        <li>
          <p>Which type of ticket do you have?</p>
          <a href="#tab3">Type 1</a>
          <a href="#tab3">Type 2</a>
          <a href="#tab3">Type 3</a>
          <a href="#tab4">Type 4</a>
          <a href="#tab4">Type 5</a>
          <a href="#tab5">Type 6</a>
          <button type="button">Back</button>
        </li>
      </ul>

      <ul class="tab" id="tab2">
        <li>
          <p>Purchase a ticket and come back later</p>
          <button>Back</button>
        </li>
      </ul>

      <ul class="tab" id="tab3">
        <li>
          <p>Proceed to Hall A</p>
          <button>Back</button>
        </li>
      </ul>

      <ul class="tab" id="tab4">
        <li>
          <p>Head to Hall B</p>
          <button>Back</button>
        </li>
      </ul>

      <ul class="tab" id="tab5">
        <li>
          <p>Make your way to Hall C</p>
          <button>Back</button>
        </li>
      </ul>

    </section>

  </body>
</html>

CSS

.question_wrapper {
  display: flex;
  justify-content: center;
}

ul {
  list-style: none;
  border: 1px solid black;
  text-align: center;
  padding-left: 0;
  width: 40rem;
  height: 20rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tab {
  display: none;
}
.tab.active {
  display: flex;
}

JS/JQuery

$(document).ready(function(){
  $(function() {
    $(".tab a").click(function() {
      $(this).parent().addClass("active").siblings(".active").removeClass("active");
      var tabContents = $(this).attr("href");
      $(tabContents).addClass("active").siblings(".active").removeClass("active");
      return false;
    });
  });

  $("button").on("click", function(){
    $(this).parent().removeClass("active");
  });

});




Answer №1

I've implemented a few modifications to the button click functionality.

  • Changed from using $(':button') to just $('button')
  • Now, when clicked, it removes the active class from all ul elements except for the first one

It appears that there is an error in your code where each ul only contains one li element. For every item under ul, a li should be used and additional elements can also be included if needed.

Update:

The snippet has been updated with the feature to navigate back to the previous ul instead of always returning to the first one.

An extra attribute has been added to each ul to determine their level. So, clicking on the back button will identify the current level and locate the next level based on that information.

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

Tips for successfully transmitting an HTML form array using jQuery's .post method

Looking for assistance with using $.post() method to send form data. Here is an example of a simplified form containing a multi-dimensional array: <form action="save-objectives.php" name="save-objectives-form" id="save-objectives-form"> <input ...

The complete page gets re-rendered when Nuxt child routes are used

When I attempt to utilize child routes, my goal is to maintain specific data on the page while modifying other content. To illustrate this concept, I have created a straightforward example available at this link. After selecting "cat" and increasing the ...

the best way to transfer a JavaScript value to a PHP page

I am experiencing an issue with an undefined function in my PHP page. I have tried everything and it just doesn't seem to work for me. Can anyone please assist me with this problem? <!DOCTYPE html> <html> <head> <scrip ...

Making changes to a variable or option through the props in a different file

In the index.js file, I have implemented getStaticProps() to fetch data from an exported function. This function requires 2 parameters, one for the type of listing and the quantity. export async function getStaticProps() { const type = 0 const data = a ...

Issue with the alignment of DIV container due to spacing issues

In the screenshot below, there is a DIV container with padding of 15px on the left and right. However, the contents are appearing spaced out downwards for some reason... Other than the left & right padding, no other styling seems to be affecting it. Adjus ...

What is the best way to define relative paths for content like CSS files?

Whenever I link to a file, I always find myself having to include the entire absolute path like this <link rel="stylesheet" type="text/css" href="F:/XmppOld/XAMPP2/htdocs/MAIN_SITE_LAYOUT/css/stylemainpage.css" /> I want to simplify it to ...

Codeigniter - Ajax request successful in remote server but fails in local server

I am encountering an issue with my web application that makes Ajax requests to a server using Codeigniter-php code. While the Ajax requests work fine on the local server, they do not function properly when the application is hosted on a remote server. The ...

Distinguishing Between URLs for Google Maps JavaScript API

Can you explain the discrepancy between two Google API URLs? One is https://maps.google.com/maps/api/js?key={api_key}, which is currently not functioning and returns an error when attempting to use it on a map to display coordinates. The other is https:/ ...

Tips on stopping tags from being deleted in select2

Is there a way to customize a select2 widget with tags:true so that tags can only be removed by clicking the "x" button in the tag instead of simply clicking on the selected item in the drop-down menu? ...

How to Make Buttons Vanish and Reappear

Check out this fiddle for a picture button 'scroller' that I created. It may not be perfect, but my main focus is on making the arrow buttons fade in and out when reaching the end of the picture order. I am considering using a JavaScript functio ...

Is it possible to utilize arrow functions in Vue while using style binding?

As I venture into the world of Vue JS HTML templates, I am exploring how to bind styles using arrow functions. My goal is to toggle the visibility of a div that originates from the Vuex store. Below is my current attempt at achieving this. The main_activi ...

Using JQuery to test for element visibility in Jest tests

I am currently facing an issue in my unit test where the visibility state of an element does not change as expected. I am using .is(":visible") to verify this, and while it works fine in browsers, the unit test always reports that the element is hidden. B ...

What is the best way to ensure that the bootstrap nav tab content fits perfectly on one line?

Check out this bootstrap navbar example You can see a screenshot here. <ul class="nav nav-tabs" style="display: inlne-block"> <li class="nav-item" style="text-align: center; display: inline;"> <div> <a class="nav ...

Guide to transmitting webcam feed from server to servlet

I am trying to display the webcam connected to my server in a servlet. I have come across suggestions to use getUserMedia();, however, this only captures the user's video webcam feed and not the server's. Is there a way to achieve this? My servl ...

Convert HTML Form to PDF or Docx with Angular 4

My current setup involves Angular 4 with Node and MySQL. I have a form that, upon submission, needs to trigger a Save As... dialog box for the user to save the entered form contents as either a PDF or Word Doc. I've attempted using htmlDox and saveAs ...

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

Is it possible that my PHP script will not accept my POST request if I initially

Recently, I developed a dynamic signature generator for my online game. To create your own signature, you can manually access it via I attempted to simplify the process by creating a userbox where users don't have to visit and edit it. My goa ...

Is there a way to access the badge hidden behind the collapsible menu in bootstrap 4?

After moving from bootstrap 3 to bootstrap 4, my items no longer align properly. I've scoured the entire Internet for a solution, but I've run out of options (and patience.. haha) This is how it currently looks: https://i.sstatic.net/ra22j.png ...

Objects within the Prototype in Javascript

While delving into the world of AngularJS, I stumbled upon an interesting revelation - when objects are placed in a prototype object, any instances inheriting from that prototype will alter the prototype's objects upon assignment. For example: funct ...

Is it advisable to compress my API response in PHP?

At this stage, I find myself needing to generate extensive reports in order to gain a better understanding of the data at hand. To do so, I must retrieve one of my tables which contains around 50 parameters and 40,000 rows. While fetching the data via API ...