Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:40:08+00:00 2026-06-14T03:40:08+00:00

Well, one more question. Since I started learning javascript short time ago, I am

  • 0

Well, one more question. Since I started learning javascript short time ago, I am almost obsessed trying new things! Here it goes:

Let’s say that I have an array of strings and I want to iterate on it with a navigation menu with the buttons FIRST, PREVIOUS, NEXT, LAST.

Look at this code:

var thearray = ["article1", "article2", "article3"];

var thebody = document.getElementsByTagName('body')[0];
var divcontainer = document.createElement("div");
var divpage = document.createElement("div");

function generatepage(article) {
    var paragraph = document.createElement("p");
    var name = document.createTextNode(thearray[article]);
    paragraph.appendChild(name);
    divpage.appendChild(paragraph);
}

divcontainer.appendChild(divpage);
thebody.appendChild(divcontainer);


generatepage(0); // that would be for the first article

I also figured out that generatepage(thearray.length -1)would be the call for the last article, so I have solved two buttons (before generating new content I would erase it with innerHTMLbut what I cannot think about how to do are the PREVIOUS and NEXT buttons…

Do you have any suggestion about how should I get started to make working PREVIOUS and NEXT?

I attach a JSFiddle

Thank you so much for any advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:40:09+00:00Added an answer on June 14, 2026 at 3:40 am

    You can save the active page in a variable outside the function:

    var page = 0;
    

    Then you don’t need to bring any page into generatepage():

    function generatepage() {
        var paragraph = document.createElement("p");
        var name = document.createTextNode(thearray[page]);
        paragraph.appendChild(name);
        divpage.appendChild(paragraph);
    }
    

    Now you can control the page from outside the function:

    var next = function() {
        if ( page < page.length-1 ) { page++; }
    }
    
    var prev = function() {
        if ( page ) { page--; }
    }
    

    So to show the first page:

    page = 0;
    generatepage()
    

    And the next:

    next();
    generatepage()
    

    etc…. There are other ways too of course but this might give you an idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I was trying to select rows from one table if there are no
This is more a curiosity question than a pressing one. This question is looking
ok guyz, got one more question for you. i need to select only the
Since the last time I asked a question I've gotten some really good answers,
This is actually a much more concise, much more clear question than the one
I have a long piece of PL/SQL which is working well except for one
Well basically, one of my mates was practicing JS and he had an idea
Well as I have posted earlier too...I have created a site in two languages.One
Well, I know how to handle associations from one model (call it ModelA) through
Well the title is quite self explanatory. I want to build two panels one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.