Skip to content

Instantly share code, notes, and snippets.

@infoscigeek
Created December 18, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infoscigeek/ebf3abf936f657c354c783d097d76e14 to your computer and use it in GitHub Desktop.
Save infoscigeek/ebf3abf936f657c354c783d097d76e14 to your computer and use it in GitHub Desktop.
Using Javascript to load a new div without reloading the entire page.
<a name="blogfeed"></a>
<div class="usbln-blog-toggle" aria-hidden="true">
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-dobe');">DOBE</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-events');">Events</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-news');">News</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-partners');">Partners</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-students');">Students</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-videos');">Videos</a>
<a class="usbln-blog-toggle-item" href="#blogfeed" onclick="toggleVisibility('usbln-all');">All</a>
</div>
<div class="usbln-blog-main">
<div class="usbln-blog-feed">
<div id="usbln-all">
<?php
//list 10 latest posts in the "Blog" category
?>
<a href="#blogfeed" onclick="toggleVisibility('usbln-all-more');"><span class="usbln-blog-feature-readmore">Load More</span></a><br class="clear" />
</div>
<div id="usbln-all-more" style="display: none;">
<?php
//list all latest posts in the "Blog" category
?>
</div>
<div id="usbln-events" style="display: none;">
<?php
//list all latest posts in the "News" category
?>
<?php//option to view next 10 posts?>
</div>
</div>
</div>
<script type="text/javascript">
var divs = ["usbln-all", "usbln-all-more", "usbln-news", "usbln-news-more","usbln-events", "usbln-partners", "usbln-students", "usbln-dobe", "usbln-videos"];
var visibleDivId = null;
function toggleVisibility(divId) {
if(visibleDivId === divId) {
//visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for(i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if(visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment