Display each div one at a time

I am working on a script that should reveal my divs step by step. However, the current code only shows all the divs at once when clicked. How can I modify it to detect each individual div and unveil them one by one? For example, on the 1st click => expand div for step 1, and on the 2nd click => expand div for step 2. Once all the divs are expanded, clicking the button should hide them all.

Thank you

<div id="stepshow1">
  <h3>Step 1</h3>
</div>
<div id="stepshow2">
  <h3>Step 2</h3>
</div>

<button id="stepbtn"></button>

<script>
  var qqcounter = 1;
  var maxstep = 3;
  $("#stepbtn").text("Show step " + qqcounter);
  $(".stepshow").hide();

  $("#stepbtn").click(function(){
    qqcounter++;
    if(qqcounter <= maxstep)
    {
      $("#stepbtn").text("Show step " + qqcounter);
      $("#stepshow" + qqcounter).slideDown();
    }
    else
    {
      $("#stepbtn").text("Hide steps");
      $(".stepshow").slideUp();
    }
  }); 
</script>

Answer №1

Check out this example for demonstrating show/hide functionality:

http://jsfiddle.net/rVwkn/14/

Using jQuery:

$('#stepbtn').click( function() {  
  if ($('#step2').is(':visible')) {
      $('#step3').show();
      $('#stepbtn').hide();
  } else if ($('#step1').is(':visible')) {
      $('#step2').show();
  }
});

$('#backbtn').click( function() {  
  if ($('#step3').is(':visible')) {
      $('#step3').hide();
      $('#stepbtn').show();
  } else if ($('#step1').is(':visible')) {
      $('#step1').hide();
  }
});   

CSS Styles:

#step2 {
    display:none;
}
#step3 {
    display:none;
}
#backbtn { 
    display: none;    
}

HTML Markup:

<div id="step1">
  <h3>Step 1</h3>
</div>
<div id="step2">
  <h3>Step 2</h3>
</div>
<div id="step3">
  <h3>Step 3</h3>
</div>

<button id="stepbtn">Next Step</button>
<button id="backbtn">Go Back a Step</button>

Updated Version:

For added flexibility, here is another version of the example:

http://jsfiddle.net/ddr6D/6/

Using jQuery:

$(document).ready( function() {
    var current_step = 1;
    var max_number_of_steps = 6;
   $('#stepbtn').click( function() {  
      var next_step = current_step + 1;
      $('#step'+next_step).slideDown();
      $('#backbtn').show();
      current_step++; // increase the step we are on...
      if (current_step == max_number_of_steps) {
        $('#stepbtn').hide();
      }
   });

   $('#backbtn').click( function() {  
      $('#step'+current_step).slideUp();// hide it first,
      current_step--; // update the correct step
      if (current_step == 1) {
        $('#backbtn').hide();
      }
       if (current_step < max_number_of_steps) {
           $('#stepbtn').show(); // show when hidden...
       }
   }); 

});

CSS Styles:

#step2,#step3,#step4,#step5,#step6 {
    display:none;
}
#backbtn { 
    display: none;
}

HTML Markup:

<div id="step1">
  <h3>Step 1</h3>
</div>
<div id="step2">
  <h3>Step 2</h3>
</div>
<div id="step3">
  <h3>Step 3</h3>
</div>
<div id="step4">
  <h3>Step 4</h3>
</div>
<div id="step5">
  <h3>Step 5</h3>
</div>
<div id="step6">
  <h3>Step 6</h3>
</div>

<button id="stepbtn">Next Step</button>
<button id="backbtn">Go Back a Step</button>

Answer №2

Website Structure

<div id="stepshow1" class="stepshow">
            <h3>Step 1</h3>
        </div>
  <div id="stepshow2" class="stepshow">
            <h3>Step 2</h3>
        </div>
  <div id="stepshow3" class="stepshow">
            <h3>Step 3</h3>
        </div>

        <input type="button" id="stepbtn" />

Programming Logic

var qqcounter = 0;
var maxstep = 4;
$("#stepbtn").text("Show step " + qqcounter);
$(".stepshow").hide();
$("#stepbtn").attr('value', 'Show step' + (qqcounter+1));
$("#stepbtn").click(function () {
    console.log(qqcounter);
    qqcounter++;
    if (qqcounter < maxstep) {
        $("#stepbtn").attr('value', 'Show step' + (qqcounter + 1));
        $('#stepshow' + qqcounter).slideDown();
    }
    else {
        $("#stepbtn").attr('value', 'Hide step');
        $(".stepshow").hide();
        qqcounter = 0;
    }
});

Answer №3

The current code will not work because both elements have the same id "stepshow." To fix this, you should change one of the ids to something unique like "stepshow_1". Here's an example:

$('btn').click(function(){

    $('#stepshow_'+counter).show();
    counter++;

});

You can view a working example in this fiddle http://jsfiddle.net/uD3W2/2/

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

Leveraging jquery's $.ajax method to send GET values to a PHP endpoint

Is it possible to use an AJAX call to pass a value to a PHP script? For example, if I have the URL example.com/test.php?command=apple, how can I make sure that the code is executed properly on the server side? This is how my code looks like: PHP: <?p ...

Tips for successfully installing a package through npm

Looking to set up nest.js Using the guide provided below. https://www.npmjs.com/package/@nestjs/cli Attempted the following command $ npm install -g @nestjs/cli Encountered this error message. bash: /usr/local/bin/npm: No such file or directory Any ...

Experience the sleek and intuitive Material Design Lite navigation drawer, inspired by the likes of Google Android

Currently, I am incorporating Material design lite into my website and have found many templates that work well. However, I am looking to make a slight design modification to the navigation drawer. Here is the dashboard template I am currently utilizing: ...

Empty body detected in Jquery AJAX request with Django REST running in a Docker environment

Using JavaScript to load a template called index.html from the /static directory. The JavaScript code in app.js: var obj = new Object(); obj.startTime = "123"; obj.endTime = "456"; console.log("fetchNext "+JSON.stringify(obj)); v ...

When utilizing "reques" in Node.js code, the parameter response.timings may be found to be undefined

The code I created in node.js is giving me trouble - for some reason, response.timing is showing up as undefined. Any idea what could be causing this issue? const request = require("request"); request.get({ time : true, url : 'https://www.bbc.com ...

CSS can be used to eliminate specific background images from a webpage

In the CSS code, I have specified two background images within a 'body' tag. #body { background: url("image1.jpeg"), url("image2.png"); background-repeat: no-repeat, no-repeat; } But now, I need to remove only "image1" from a specific p ...

The initiation of jQuery animation through user interaction hinges on the completion of the preceding animation

In my project, I've designed a timeline that offers users the ability to zoom in and out by simply clicking on corresponding buttons. Since the timeline is too large to fit entirely on the screen, it is contained within a scrollable div. To ensure tha ...

Having trouble with a React child component not reacting to a click event?

The current issue I am experiencing is that the component is not triggering a click event when the button is clicked. TickerPage render() { return ( <div className="TickerPage"> <div className="TickerPage-container"> <b ...

How can I utilize the parseFloat feature within my dedicated parseFloat function located in an angular factory?

What is the solution for accessing parseFloat within an angular factory function named parseFloat? angular .module('myApp') .factory('mathService', [MathService]); function MathService() { return { parseFloat: myGl ...

Encountering Karma Angular Error: Name 'X' Not Found

After executing Karma Start in my Angular project, I am encountering several errors. All the error messages highlight issues like 'Cannot find name Blob', 'Cannot Find name KeyboardEvent', 'Cannot find name HTMLElement', amon ...

Tips for ensuring the alignment of a table header and body once data has been loaded into it

I am currently developing an Angular 7 application where I am trying to populate a table with data retrieved from a web service. However, the issue I am facing is that the headers of the table do not align properly with the body content after loading data ...

Different methods to obscure solely URLs in AngularJS

Is there a way to effectively obfuscate URLs in AngularJS? Currently, I am using base 64 encoding as a method of obscuring my URLs. For example, let's say my URL is: I encode and decode it like this: aHR0cDovLzE5Mi4wLjAuMC9teS91cmwv However, when ...

Pinia: Is it better to invoke '{myStore}' or 'return myStore' within the return statement?

I'm currently working on integrating a Pinia store into a component of my Vue app and I'm puzzled by the need to return the store in { } instead of just returning it as is. Can someone clarify the distinction between return {foo} and return foo f ...

Reactjs: Prop Type Error - Issue with retrieving the values from props during data submission

Currently, I am developing a social app where users can post screams, like posts, and add comments. The issue I'm encountering is that I am not receiving the props value when posting a new scream initially, but after refreshing the page, everything wo ...

Vue.js is rendering the chart inaccurately in dc.js

I am currently facing an issue with using dc.js inside a Vue component. My linechart has this filled black area and the brush tool looks different from the normal brush tool. I took this code from my plain JavaScript project. <template> <div> ...

Unable to show the table row color using Angular in Internet Explorer

I've customized a table by changing the row color based on a property value, and I'm using ng-repeat to display the rows. Here's my table: <table class="tab table span6 tabhover" style="margin:0;" > <thead> ...

The AngularJS Factory $http encounters an error when attempting to read the property 'length' of an undefined value

I am looking to implement a factory in my REST Controller that will return an array of Strings. I want to be able to reuse this function in my services.js file. Here is the HTML for an Autocomplete input field: <input type="text" ng-model="vertrag.ver ...

Using jQuery to automatically scroll to the bottom of a div when sliding down

When a user clicks on a link to slide the div down, I want it to automatically scroll to the bottom of the div. I've attempted to use scrollTo and animate methods to achieve this effect. $('html, body').animate({ scrollTop: $("#elementID") ...

Utilizing border-radius specifically on the ul element to apply rounded corners to only the outer li items

I'm exploring a concept where I want to create a curved border around the list items, both on the outside and inside. Here are some examples: Right Check out the correct curved border here Wrong: See the incorrect curved border here Please note, ...

Angular JS element cannot be located

My Angular object is not being passed when I write a new function, and I'm unsure why. The object, named person, is directly bound in the HTML and returns 2 items from the cardsCollection array. The function I'm attempting to create is called cl ...