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

  • Home
  • SEARCH
  • 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 8714383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:35:07+00:00 2026-06-13T05:35:07+00:00

I have a jquery script which replaces 1 div with another and replaces 1

  • 0

I have a jquery script which replaces 1 div with another and replaces 1 link with another when it is selected:

$('.child2, a[href="#1"]').hide()

$('#replacepagelinks a').click(function(){
     $(this).hide().siblings().show()
     var w = this.hash.replace('#', '');
     $('#parent div.child'+w).show().siblings().hide()
})

Here is my html:

<div id="parent">
   <div class="child1">Child 1 Contents</div>
   <div class="child2">Child 2 Contents</div>
</div>
<div id="replacepagelinks">
     <a href="#1">Replace Child 1 with Child 2</a>
     <a href="#2">Replace Child 2 with Child 1</a>
</div>

Please check out this jsfiddle http://jsfiddle.net/KaqZ5/ for a working example.

This works great, however i need it to work with up to 5 divs like so:

  1. In child1 i would like only <a href="#1">Replace Child 1 with Child 2</a>

  2. In child2 i would like <a href="#2">Replace Child 2 with Child 1</a> and <a href="#3">Replace Child 2 with Child 3</a>

  3. In child3 i would like <a href="#3">Replace Child 3 with Child 2</a> and <a href="#4">Replace Child 3 with Child 4</a>

  4. In child4 i would like <a href="#4">Replace Child 4 with Child 3</a> and <a href="#5">Replace Child 4 with Child 5</a>.

  5. In child5 i would like only <a href="#5">Replace Child 5 with Child 4</a>

So each page has a ‘next’ and ‘back’ link except for the first page which only has a ‘next’ link and the last page which only has a ‘back’ link and i need this to support 2 child div’s, 3 child div’s and 4 child div’s as well.

My experience with jquery is almost nil so if somebody can please update my jsfiddle i would be very grateful.

  • 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-13T05:35:07+00:00Added an answer on June 13, 2026 at 5:35 am

    See the updated fiddle at http://jsfiddle.net/Palpatim/KaqZ5/7/

    Revised HTML:

    <div class="menu">
        <div class="menu_item">
            <div class="parent">
                <div class="child1">Child 1 Contents</div>
                <div class="child2">Child 2 Contents</div>
                <div class="child3">Child 3 Contents</div>
                <div class="child4">Child 4 Contents</div>
                <div class="child5">Child 5 Contents</div>
            </div>
            <div class="nav">
                <a class="prev" href="#">Back</a>
                <a class="next" href="#">Next</a>
            </div>
        </div>
    
        <div class="menu_item">
            <div class="parent">
                <div class="child1">Child 1 Contents</div>
                <div class="child2">Child 2 Contents</div>
                <div class="child3">Child 3 Contents</div>
            </div>
            <div class="nav">
                <a class="prev" href="#">Back</a>
                <a class="next" href="#">Next</a>
            </div>
        </div>
    </div>
    

    New CSS

    /* Initially hide all menu items */
    .parent div {
        display: none;
    }
    
    /* Show active item */
    .parent div.active {
        display: block;
    }
    
    /* If a nav link is disabled, color it grey and set the cursor to an arrow */
    a[disabled="disabled"] {
        color: #999;
        cursor: default;
    }
    

    ​Revised JavaScript

    // Set the initial menu state: Each 'prev' link is disabled and the first menu item 
    // for each menu block is active
    $('a.prev').attr('disabled', 'disabled');
    $('.parent div:first-child').addClass('active');
    
    $('a.next').click(function() {
        // Find the parent menu container 
        // - parent of (this) is div.nav
        //   - parent of div.nav is div.menu_item
        var menu = $(this).parent().parent();
    
        // Find the currently active menu item
        var active = $('.active', menu);
    
        // Find the next menu item in the list
        var nextItem = active.next();
    
        // If there is a next menu item, make it active
        if (nextItem.length) {
            // Make current item inactive
            active.removeClass('active');
            // Make new item active
            nextItem.addClass('active');
    
            // If there's no item after the new item,
            // disable navigation
            if (!nextItem.next().length) {
                $('a.next', menu).attr('disabled', 'disabled');
            }
    
            // If we just clicked 'next', we can enable the 'prev' button
            if ($('a.prev', menu).attr('disabled') == 'disabled') {
                $('a.prev', menu).removeAttr('disabled');
            }
        }
    });
    
    // Pretty much same as above, with the directions reversed
    $('a.prev').click(function() {
        var menu = $(this).parent().parent();
        var active = $('.active', menu);
        var prevItem = active.prev();
        if (prevItem.length) {
            active.removeClass('active');
            prevItem.addClass('active');
            if (!prevItem.prev().length) {
                $('a.prev', menu).attr('disabled', 'disabled');
            }
            if ($('a.next', menu).attr('disabled') == 'disabled') {
                $('a.next', menu).removeAttr('disabled');
            }
        }
    });
    

    Here are some important changes I made from your original:

    • Use CSS to hide menu items and re-display them using an .active class. That makes the markup cleaner, and allows you to style
    • Use CSS to visually disable the navigation links, rather than suppressing them altogether. You can of course simply hide them, but if it’s truly a user navigation, then you want them to be able to see that the link is simply not applicable, rather than removing it from their view altogether.
    • Restructured the navigation to put “Back” before “Next” visually. If you truly want to remove it, you can simply change the CSS definition of the disabled anchors.
    • Changed your links to have classes instead of

    Some other optimizations you should consider:

    • Refactor the guts of the navigation code into a standalone function. It’s nearly the same whether you’re going forward or backward, so consider making the function more abstract by passing in the parent menu element and a desired direction.
    • Use a jQuery pagination plugin, like http://www.xarg.org/2011/09/jquery-pagination-revised/ These have the benefit of greater functionality, and in the case of actively developed plugins, they’re likely to be better tested than you’ll be able to do on your own.
    • You have two navigation blocks on your page, but each of them have id attributes which conflict. ids must be unique on a page. I changed those to classes instead.
    • Consider the case where a menu has one item, or no items. This code will choke on those situations

    There are probably a host of other ways to do this, but this should get you started. For futher reading, I recommend perusing the jQuery documentation.

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

Sidebar

Related Questions

I have jquery script which replaces a div when a href=#1 is selected. a
I have the following jquery script: $(function(){ $('#top-menu').on('click', 'a.change-menu', function(e){ e.preventDefault() $(#menu-change-div).load($(this).attr(href)); }); });
I have a jquery script which replaces a div (child1 with child2) when a
I have this jquery script which suppose to hide/show div element base on the
I have a jQuery script, in which I am going to hide the div
I have found and customized this JQuery script, which displays different content when different
I have HTML which includes scripts in following order <script src=../static/js/jquery.js type=text/javascript> <script src=../static/js/bootstrap.js
I have this jQuery script that works fine: $(select).change(function () { var finalPrice =
I have a simple jQuery script which pushes the footer to the bottom of
Basically, I have one div(divNew) which is being populated with content from another page(thread.php)

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.