step-by-step tracking indicator

I need help figuring out how to change the buttons on my progress bar when users click the next button. Essentially, I want each step of the progress bar to lead to a different page with unique content - like buttons on step 1, text on step 2, and so on.

If you're having trouble visualizing what I'm talking about, check out this Codepen example that demonstrates the desired result: https://codepen.io/vajahath_ahmed/pen/xEgOdp. Notice how clicking "next" changes the displayed text; that's exactly what I'm aiming for in my project.

$(document).ready(function() {
  $('.step').each(function(index, element) {
    // element == this
    $(element).not('.active').addClass('done');
    $('.done').html('<i class="icon-ok"></i>');
    if ($(this).is('.active')) {
      return false;
    }
  });
  $('.step>i.icon-ok').hide();
});

const iconClasses = ['far fa-hand-pointer', 'fas fa-mortar-pestle', 'fas fa-shipping-fast', 'fas fa-shopping-cart'];

function next() {
  //console.log("Next", Math.random());
  let latestActiveStep = $("div.step.active").not(".done");
  let stepNumber = +$(latestActiveStep).data("stepnum");
  console.log("step", stepNumber);
  $(latestActiveStep).addClass("done");
  $(latestActiveStep).find("i.icon-ok").toggle();
  $(latestActiveStep).find("i").not(".icon-ok").toggle();
  $(latestActiveStep).next().addClass("active");
}

function previous() {
  //...
  
</body>

</html>

Answer №1

If you utilize the data-stepnum attribute as a counter, it can be used to trigger different divs. Take a look at this:

1. Generating content divs.

For each tab, create a content div with the class `step-content` and the attribute `data-stepnum`. Ensure that the value of the data-stepnum matches each tab number. This attribute is crucial in linking the tab with its respective content. Note how the `active` class is added to the first div, indicating the initial active content tab.
<div id="steps-content">
  <div class="step-content active" data-stepnum="0">
    <h2>Step content 1</h2>
  </div>
  <div class="step-content" data-stepnum="1">
    <h2>Step content 2</h2>
  </div>
  <div class="step-content" data-stepnum="2">
    <h2>Step content 3</h2>
  </div>
  <div class="step-content" data-stepnum="3">
    <h2>Step content 4</h2>
  </div>
</div>

2. Concealing content divs using CSS

The content divs are initially hidden using CSS, except for those with the `active` class.
.step-content{
  display: none;
}

.step-content.active{
  display: block;
}

3. Incorporating the trigger with jQuery

Since you already have the `data-stepnum` value stored in the variable `stepNumber`, you can use this to apply the active class to the relevant content.

We will modify the functions for `next()` and `previous()`. Initially, let's define a variable with the content class name for convenience.

var stepContentClass = '.step-content';

Next, update the `next()` function to remove all active classes before displaying the correct content based on the saved `data-stepnum` value.

$(stepContentClass).removeClass('active');

Repeat the same process for the `previous()` function

Complete functions below:

// Functions here...

Outcome

View the complete code on JSFiddle: https://jsfiddle.net/0p9vekm7/

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

What is the best way to isolate particular components of an argument and store them in separate variables?

Currently, I am facing a challenge in extracting the name and id of an emoji from a discord argument using discord.js. The input provided to me is <:hack_wump:670702611627769876>, and my goal is to retrieve var id = '670702611627769876' alo ...

What could be the reason for the malfunctioning of the Angular options tracking by ID feature?

I seem to be facing an issue with my code: There is an object that holds the id of an item, along with an array containing these items. I require a 'select' element to display the currently selected item and allow for changing the selection. Th ...

Combining Various Input Types for Seamless Integration: Getting Files and Text to Collaborate Efficiently

In my form, I have two types of image inputs - one from a file and the other from a URL. To ensure that the image from the last changed input is used, I created an additional invisible input field called "imgTempURL". This field is filled with the image&ap ...

Error: This value is not a valid HTMLInputElement type (Google Maps)

When attempting to create a basic autocomplete field for Google Map places, I encountered the following error message despite having an HtmlInputElement as my field InvalidValueError: not an instance of HTMLInputElement Here is the HTML code: <inpu ...

Rotate the d3.js datamap by dragging using your cursor

Recently, I created a custom map using datamaps developed by @markmarkoh I'm thrilled with how it turned out, but I am eager to add functionality that allows the map to rotate when dragged with a cursor. My inspiration comes from examples like the on ...

The process of a ReactJS component's lifecycle is affected when an onClick event triggers a fetch function, causing the state

For the past week, I've been grappling with a coding challenge. My goal is to create a basic Pokedex featuring the original 151 Pokemon. The list of Pokemon should be displayed on the right side of the screen, pulled directly from a .json file copied ...

Supervising the organization of HTML sections for an offline web application

Currently, I am developing an offline HTML5 application that involves a significant amount of DOM manipulation through the creation of HTML strings within JavaScript functions. For example: var html=''; html+='<div class="main">&apos ...

Possible rephrased version: "Strategies to halt the playback of a screencast.com video that is

I recently uploaded a screencast video to my website and now I'm looking for a way to pause the video when I click on a button. I attempted using some JavaScript code with an onclick event attached to the button. var myVideoPlayer = document.getEleme ...

Running a <script> tag with an external src attribute in a dynamic manner through the use of eval

Currently, I am utilizing the Genius API to fetch lyrics for a particular song and then embed them within an HTML <div> tag. My interaction with this API is through PHP, employing an AJAX GET request. Upon a successful AJAX request, the following HT ...

How can one stop users from navigating away from a webpage using the back button on their browser?

1) I'm currently exploring options to display a confirm pop-up when a user tries to leave a page using the browser's back button. It seems that the unload event does not trigger in this scenario. My application is built using angular2. I have att ...

Pop-up alert for text sections that are longer than a specific character limit

As I work on a website featuring dynamically generated blog posts of varying lengths, I'm looking to restrict the height of each post to 250px. Should a post exceed this limit, I want to truncate it and include a "read more" link that opens a modal ov ...

Expression validation in AngularJS is a powerful tool for ensuring that

Attempting to implement input validation using an Angular expression because validation data will be sourced from the database. Attempting the following code: controller vm.key="ng-required" vm.value="true" html <input type="text" name="fi ...

Error caused by MongoClient TypeError

As a newcomer to NodeJS and someone exploring Dependency Injection for the first time, I encountered an error that led me to seek help. Before asking my question, I reviewed some similar threads on Stack Overflow: [1][2] Upon running my main code, I recei ...

What is the best method for identifying the destination of my data submission in an HTML form?

I am currently facing an issue with a website that has an input form. I am struggling to identify where the form posts its data to. There must be some PHP code handling the form, whether it's using JSON or direct post. Is there a way to find this in ...

refreshing the webpage's content following the completion of an asynchronous request

I am working on an Ionic2 app that utilizes the SideMenu template. On the rootPage, I have the following code: export class HomePage { products: any = []; constructor(public navCtrl: NavController, public navParams: NavParams, private woo: WooCommer ...

Detecting collisions between a cube and sphere using three.js: A guide

There are two basic 3D shapes, a sphere and a cube, in my current scene. I am seeking a method to determine whether they are in collision with each other or not. Could you advise me on how to accomplish this task? ...

Typescript input event

I need help implementing an on change event when a file is selected from an input(file) element. What I want is for the event to set a textbox to display the name of the selected file. Unfortunately, I haven't been able to find a clear example or figu ...

Problem with Jquery hover or mouse enter show/hide functionality

I am currently experimenting with displaying hidden text when the mouse enters a div and hiding it when it leaves. Here is the progress I've made on my JSFiddle: $(document).ready(function() { $(".image").mouseover(function(){ $(".hover").show ...

Unable to find the designated ./uploads directory while attempting to upload a file in Express JS

I'm currently working on a project with the following structure: -app -uploads -client - dropzone.js -server.js My goal is to upload a file using dropzone js, Express JS, and React JS. However, whenever I try to drop a file in ...

WebGL annotations failing to display on the webpage

I'm currently working on displaying a 3D model using Three.js in a web browser and looking to incorporate annotations like the ones shown in this Sketchfab model: Interactive 3D Test. I came across a helpful guide at this link, but I'm facing iss ...