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 8831919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:24:58+00:00 2026-06-14T08:24:58+00:00

I have a project that I borrowed some JavaScript from a lab that I

  • 0

I have a project that I borrowed some JavaScript from a lab that I did in class a year ago and I’m a little rusty so I can’t figure out why it isn’t working correctly. I made an example site so you can see how it’s not working. It is located at amykate.com/test and the example files can be downloaded at amykate.com/test.zip.

I need it set up so that when you first navigate to the site there would be no content showing from the “main” div. This allows for the slideshow I’ll have going in the background to be the main focus. Once you click on a link though, it should show the appropriate content from the associated div overtop of the slideshow. (I left all the slideshow stuff out for the example because it isn’t necessary.) The problem I am having is that once you click on a link, you have to then refresh the page to actually see the content. Why is that? I wanted to be able to not have to refresh the page at all so that the slideshow can just continue in the background the whole time. Any suggestions?

Here is how the HTML structure is set up…

<nav id="hide-show-nav">
    <a href="#about">About</a> |
    <a href="#gallery">Gallery</a> |
    <a href="#contact">Contact</a>
</nav>

<div id="main">

    <div id="about">
        <p>This is the about section.</p>
    </div> <!-- #about -->

    <div id="gallery">
        <p>This is the gallery section.</p>
    </div> <!-- #gallery -->

    <div id="contact">
        <p>This is the contact section.</p>
    </div> <!-- #contact -->

</div> <!-- #main -->

And the JavaScript goes like this…

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addEvent(element, type, handler)
{
    if (element.addEventListener)
        element.addEventListener(type, handler, false);
    else
    {
        if (!handler.$$guid) handler.$$guid = addEvent.guid++;
        if (!element.events) element.events = {};
        var handlers = element.events[type];
        if (!handlers)
        {
            handlers = element.events[type] = {};
            if (element['on' + type]) handlers[0] = element['on' + type];
            element['on' + type] = handleEvent;
        }

        handlers[handler.$$guid] = handler;
    }
}
addEvent.guid = 1;

addLoadEvent(init);


function init()
{
    findDest();

    //get reference to container element
    var holder = document.getElementById('main');

    //create ref to all divs inside of main
    var divs = holder.getElementsByTagName('DIV');
    var l = divs.length;

    //get reference to nav
    var nav = document.getElementById('hide-show-nav');

    //create ref to all links in nav
    var links = nav.getElementsByTagName('A');
    var numLinks = links.length;

    //loop over links add event listeners
    for(var j=0; j < numLinks; j++)
    {
        addEvent(links[j], 'click', showDiv);   
        addEvent(links[j], 'click', setStatus); 
    }
}


function showDiv(evt)
{
    //get reference to container element
    var holder = document.getElementById('main');

    //create ref to all divs inside of main
    var divs = holder.getElementsByTagName('DIV');
    var l = divs.length;

    //loop over divs and hide them all
    for(var i = 0; i < l; i++)
    {
        divs[i].className = 'hide'; 
    }

    //get reference to link click on
    var target = evt.target || window.event.srcElement;
    var destination = target.href;
    var divName = destination.split('#')[1];
    document.getElementById(divName).className = 'show';
}

function findDest()
{
    //get reference to container element
    var holder = document.getElementById('main');

    //create ref to all divs inside of main
    var divs = holder.getElementsByTagName('DIV');
    var l = divs.length;

    //loop over divs and hide them all
    for(var i = 0; i < l; i++)
    {
        divs[i].className = 'hide'; 
    }

    //get reference to link click on
    var destName = window.location.toString();
    var loc = destName.split('#')[1];   
    document.getElementById(loc).className = 'show';
}

function setStatus(evt)
{
    //get reference to link click on
    var target = evt.target || window.event.srcElement;

    //get reference to nav
    var nav = document.getElementById('hide-show-nav');

    //create ref to all links in nav
    var links = nav.getElementsByTagName('A');
    var numLinks = links.length;

    //loop over links add event listeners
    for(var j=0; j < numLinks; j++)
    {
        changeClassName('remove', links[j], 'statusOn');
    }

    //add status indication to link
    changeClassName('add', target, 'statusOn');

}

And this little bit of CSS…

.hide {
    display:none;
}

.show {
    display:block;
}
  • 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-14T08:24:59+00:00Added an answer on June 14, 2026 at 8:24 am

    I did finally figure this out a while ago and figured I’d post what I did incase anyone else comes across this.

    $(function() {
        $('#main').addClass('hide');
    });
    
    $('body').on('click','nav a', function(e) {
        $('#main').removeClass('hide');
        $('#main').children().addClass('hide');
        $($(this).attr('href')).removeClass('hide');
        e.preventDefault();
    });
    

    Hope that helps.

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

Sidebar

Related Questions

I have a project that using some third-party libraries. My questions is how to
I have a project that is coded in C. I can build it, change
I have a project that uses some Java and some Groovy, and it's all
I have project that creates dlls. These dlls are invoked from python later by
I have project that is giving me some headache and i think its because
How I can identify what svn branch/tag points my code? I have project that
I have a project that's been using font-face without problem for some time. Today
We have a project that generates a code snippet that can be used on
I am a little puzzled. I have project that I compile with CFLAGS=-g -O2
I have project model that has many tasks. Both project and tasks can have

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.