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:
-
In child1 i would like only
<a href="#1">Replace Child 1 with Child 2</a> -
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> -
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> -
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>. -
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.
See the updated fiddle at http://jsfiddle.net/Palpatim/KaqZ5/7/
Revised HTML:
New CSS
Revised JavaScript
Here are some important changes I made from your original:
.activeclass. That makes the markup cleaner, and allows you to styleSome other optimizations you should consider:
idattributes which conflict.ids must be unique on a page. I changed those to classes instead.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.