Ok, so I have a page with four links on it. Now rather than having the links link to a separate page I would like to stay on the same page but swap the contents of a div on this page, as it is the same page but with different content in the middle. I'm working in Wordpress and I have created the separate parts for the middle content in different files, let's call them cont_one.php, cont_two.php, cont_three.php, cont_four.php.
So how would I go about doing this should I use PHP or jQuery? And How would I create an if statement that would know if the link is clicked then swap the content? I'm not asking you to do all the work for me although I would appreciate a demo, but if you could at least tell me what I'm looking for it would be appreciated as I have no idea where to start. Thanks
Here is a simple solution to change div content with jquery
<div>
<ul>
<li id="opt1"><a href="">Title1</a></li>
<li id="opt2"><a href="">Title2</a></li>
<li id="opt3"><a href="">Title3</a></li>
</ul>
</div>
<div class="step1">CONTENT1</div>
<div class="step2">CONTENT1</div>
<div class="step3">CONTENT1</div>
And then jquery
$('#opt1').click(function(){
$('.step1').show();
$('.step2').hide();
$('.step3').hide();
});
$('#opt2').click(function(){
$('.step2').show();
$('.step1').hide();
$('.step3').hide();
});
$('#opt3').click(function(){
$('.step3').show();
$('.step2').hide();
$('.step1').hide();
});
And finally css
.step1 {display:show;}
.step2 {display:none;}
.step3 {display:none;}
You can make use of the jQuery :visible Selector (with jQuery included in the head of the page).
Here's a example for this:
HTML - LINKS
<a id="toggleit-1" href="javascript:showonlyone('hiddendiv-1');">Link #1</a>
<a id="toggleit-2" href="javascript:showonlyone('hiddendiv-2');">Link #2</a>
<a id="toggleit-3" href="javascript:showonlyone('hiddendiv-3');">Link #3</a>
<a id="toggleit-4" href="javascript:showonlyone('hiddendiv-4');">Link #4</a>
HTML - DIVs
<!-- start hidden div #1 --><div class="hiddendiv" id="hiddendiv-1"><p>1 - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></div><!-- end hidden div #1 -->
<!-- start hidden div #2 --><div class="hiddendiv" id="hiddendiv-2"><p>2 - Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ips.</p></div><!-- end hidden div #2 -->
<!-- start hidden div #3 --><div class="hiddendiv" id="hiddendiv-3"><p>3 - Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p></div><!-- end hidden div #3 -->
<!-- start hidden div #4 --><div class="hiddendiv" id="hiddendiv-4"><p>4 - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p></div><!-- end hidden div #4 -->
CSS
.hiddendiv {
display: none; // all hidden at start of the page
position: relative;
width: 95%;
margin: 0px auto 0;
padding: 10px 10px;
border: 1px solid #000;
}
jQuery
function showonlyone(thechosenone) {
$('.hiddendiv').each(function(index) {
if ($(this).attr("id") == thechosenone) { // which one was clicked ?
if($(this).is(':visible')){ // the selector checks if hidden/visible
$(this).slideUp(600); // yes = move up and hide
} else {
$(this).slideDown(600); // no = move down and show
}
}
else {
$(this).slideUp(600);
}
});
}
As stated in comments by @hungerstar, this will show you a way to achieve it If the content isn't 20 miles long [...] have it on the page initially and hide it. Then only show when clicked.
If you need to fetch content from DB or load HTML page, Ajax will be needed and script will become slightly different.