0

I want to make a button that will create a back page if I click this it will return to the last div content. I already have the function for the next page but I'm already ran out of logic for making a function for the back page button.

Here is my code for the content and my function for the next()

function next() {
  if ($('#content1').hasClass('')) {
    $('#content2').removeClass('hidden');
    $('#back').removeClass('hidden');
    $('#content1').addClass('hidden');
  } else if ($('#content2').hasClass('')) {
    $('#content2').addClass('hidden');
    $('#content3').removeClass('hidden');
  } else if ($('#content3').hasClass('')) {
    $('#content3').addClass('hidden');
    $('#content4').removeClass('hidden');
  } else if ($('#content4').hasClass('')) {
    $('#content4').addClass('hidden');
    $('#content5').removeClass('hidden');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="myDiv">
  <div id="content1">
    <h1>1</h1>
  </div>
  <div id="content2" class="hidden">
    <h1>2</h1>
  </div>
  <div id="content3" class="hidden">
    <h1>3</h1>
  </div>
  <div id="content4" class="hidden">
    <h1>4</h1>
  </div>
  <div id="content5" class="hidden">
    <h1>5</h1>
  </div>
  <button onclick='next()'>Next Page</button>
  <button id="back" onclick='back()' class="hidden">Back Page</button>
</div>

0

2 Answers 2

1

You can simplify this.

Here I remove the inline functions and have only one function.

Also no need for classes.

$(function() {
  const $container = $("div.myDiv");
  const $contents = $(".myDiv > div");
  const last = $contents.length - 1;

  $(".nav").on("click", function() {
    const isNext = $(this).is("#next"); // we clicked next
    const $cur = $container.find("div:visible"); // currently visible
    const $show = isNext ? $cur.next() : $cur.prev(); // next or prev div?
    const idx = $show.index();
    $("#prev").toggle(idx >= 1);
    $("#next").toggle(idx < last);
    $("#reveal").toggle(idx >= last);
    $cur.hide();
    $show.show();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="myDiv">
  <div id="content1">
    <h1>1</h1>
  </div>
  <div id="content2" hidden>
    <h1>2</h1>
  </div>
  <div id="content3" hidden>
    <h1>3</h1>
  </div>
  <div id="content4" hidden>
    <h1>4</h1>
  </div>
  <div id="content5" hidden>
    <h1>5</h1>
  </div>
  <button class="nav" id="prev" hidden>Previous Page</button>
  <button class="nav" id="next">Next Page</button>
  <button id="reveal" hidden>Reveal</button>

</div>

Sign up to request clarification or add additional context in comments.

4 Comments

I want to add a Reveal Button if it reaches the last page it will show the Reveal Button how do I make it using your javascript?
Same way wou hide the next. I updated
Where do I put the scrollTop or the scrollIntoView so that everytime I click the next it will automatically scroll to the top I'm using overflow: auto; in my css
It would be very useful if you updated the snippet I made you when you first asked, with all the elements and css that is relevant to your questions.. You can do $show.show(); $show[0].scrollIntoView() if you need to scroll the div into view. I need to use [0] to get the DOM element
0

You could do it like this:

function next() {
  var div = $("div[id^=content]:visible")
  var nextdiv = div.next("[id^=content]");
  if (nextdiv) {
    div.addClass("hidden");
    nextdiv.removeClass("hidden");
    $("#back").show();
    $('#next').toggle(nextdiv.next("[id^=content]").length > 0)
  }
}

function back() {
  var div = $("div[id^=content]:visible")
  var prevdiv = div.prev("[id^=content]");
  if (prevdiv) {
    div.addClass("hidden");
    prevdiv.removeClass("hidden");
    $("#next").show();
    $('#back').toggle(prevdiv.prev("[id^=content]").length > 0)
  }
}

This will make it more dynamic and easier to manage.

One thing i use here is the [id^=content] selector. That means it will only select elements where the id starts with content.

function next() {
  var div = $("div[id^=content]:visible")
  var nextdiv = div.next("[id^=content]");
  if (nextdiv) {
    div.addClass("hidden");
    nextdiv.removeClass("hidden");
    $("#back").show();
    $('#next').toggle(nextdiv.next("[id^=content]").length > 0)
  }
}

function back() {
  var div = $("div[id^=content]:visible")
  var prevdiv = div.prev("[id^=content]");
  if (prevdiv) {
    div.addClass("hidden");
    prevdiv.removeClass("hidden");
    $("#next").show();
    $('#back').toggle(prevdiv.prev("[id^=content]").length > 0)
  }
}
.hidden {
  display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myDiv">
  <div id="content1">
    <h1>1</h1>
  </div>
  <div id="content2" class="hidden">
    <h1>2</h1>
  </div>
  <div id="content3" class="hidden">
    <h1>3</h1>
  </div>
  <div id="content4" class="hidden">
    <h1>4</h1>
  </div>
  <div id="content5" class="hidden">
    <h1>5</h1>
  </div>
  <button id="next" onclick='next()'>Next Page</button>
  <button id="back" onclick='back()' class="hidden">Back Page</button>
</div>

4 Comments

@ECo Don't just "thank you" like that! Do your action
@mikenlanggio Don't "Do you action". There is nothing wrong with a "thank you". Maybe users wanner test the solutions good before accepting, and it's not uncommon for answers to be accepts a day or 2 after. Also why not explain to the OP way it's better to accept the answer rather than "yelling" at him
@mikenlanggio actually I already accepted the answer however, it says I need to wait 1 minute before accepting it :)
@ECo Don't worry. Sometimes people are sadly just in a rush. Happy to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.