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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:54:23+00:00 2026-06-04T01:54:23+00:00

I am brand new to javascript so please bear with me. I am trying

  • 0

I am brand new to javascript so please bear with me. I am trying to get a span to appear on click which I have accomplished, however I need it to return to being hidden once another link is clicked. This is what I have so far.

<html>
<head>
<style>
aside.apps{
    position:relative;
}

span.socialApp{
    visibility:hidden;
    position:absolute;
    top:20px;
    left: 0;
    background-color:#e9e9e9;
}
</style>

<script>
var state = 'hidden'; 

function showApp(a) { 

    if (state == 'visible') { 
        state = 'hidden'; 
    } 
    else { 
        state = 'visible'; 
    } 

    if (document.getElementById && !document.all) { 
        x = document.getElementById(a); 
        x.style.visibility = state; 
    } 
} 
</script>
</head>

<body>

        <aside class="apps">
            <a href="javascript://" onclick="showApp('app1');">link1</a>
                <span class="socialApp" id="app1">stuff goes here1</span>
            <a href="javascript://" onclick="showApp('app2');">link2</a>
                <span class="socialApp" id="app2">stuff goes here2</span>
            <a href="javascript://" onclick="showApp('app3');">link3</a>
                <span class="socialApp" id="app3">stuff goes here3</span>
            <a href="javascript://" onclick="showApp('app4');">link4</a>
                <span class="socialApp" id="app4">stuff goes here4</span>
        </aside>
</body>
</html>

Currently when link1 is clicked app1 appears, then once link2 is clicked app2 appears over top of link1. When link2 is then closed link1 is still visible. I need to check all 4 and make all hidden except the current selection.

  • 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-04T01:54:25+00:00Added an answer on June 4, 2026 at 1:54 am

    and welcome to Stack Overflow!

    First off, I am going to suggest you make use of jQuery assist you with your JavaScript. jQuery (and other similar frameworks like Zepto, MooTools and Dojo) irons over some of the cracks in JavaScript such as cross browser inconsistencies and will make things a lot easier. To include jQuery in your project you just need to add the following in your page’s <head> tag:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    

    Now you’ve got access to jQuery you can remove all of the onclick attributes you added to your <a> tags. The use of onclick tags is discouraged as it dates back to the early days of web development before DOM Level 1 standard was (almost) agreed upon. Instead it’s suggested you bind to the ‘click’ event – this will help to keep your HTML and JavaScript separated and in turn make your JavaScript easier to read and debug.

    jQuery makes it really easy to bind a handler (function) to a click event, you just need to use the on syntax:

    // #id is the 'id' attribute of the element you want to add a click handler to.
    $('#id').on('click', function () { alert("Clicked!"); }); 
    

    jQuery also provides a quick and easy way to show and hide elements on the page via show and hide, here how it works:

    // Again, #id is the id attribute of the element you want to show/hide.
    $('#id').show();    // Make a hidden element visible.
    $('#id').hide();    // Hide a visible element.
    

    The only thing to watch out for here is that when you ‘hide’ an element using jQuery it actually sets the display CSS attribute of the element. In your code above, you were hiding elements by toggling the visibility attribute.

    The last part of the answer lies in how we the currently visible element; this can be achieved by adding a new variable which keeps track of which element is being displayed – you can see this in my modified code below.

    <html>
        <head>
            <style>
    span.socialApp {
        /* use display: none instead of visibilty: hidden */
        display: none;
    
        position:absolute;
        top:20px;
        left: 0;
        background-color:#e9e9e9;
    }
            </style>
    
            <!-- include jQuery -->
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    
            <!-- Script for toggle apps -->
            <script>
    // Create an Array of all the app names.
    var apps = [ 'app1', 'app2', 'app3' ];
    
    // Variable which keeps track of which app is currently visible
    var visibleAppName = null;
    
    function onLinkClicked(event) {
        // Get the id of the link that was clicked.
        var linkName = event.target.id;
    
        // Strip off the '-link' from the end of the linkName
        var dashIndex = linkName.indexOf('-link');
        var appName = linkName.substr(0, dashIndex);
    
        // Call show app with the correct appName.
        showApp(appName);
    }
    
    function showApp(appNameToShow) { 
        // Hide the currently visible app (if there is one!)
        if (visibleAppName !== null) {
            $('#' + visibleAppName).hide();
        }
    
        // And show the one passed
        $('#' + appNameToShow).show();
    
        // Update the visibleApp property.
        visibleAppName = appNameToShow;
    }
    
    // $(document).ready waits for the page to finish rendering
    $(document).ready(function () {
        // Walk through the Array of Apps and add a click handler to
        // it's respective link.
        apps.forEach(function(name) {
            $('#' + name + '-link').on('click', onLinkClicked);
        });
    });         
            </script>
        </head>
        <body>
                <aside class="apps">
                <a href="#" id="app1-link">link1</a>
                <span class="socialApp" id="app1">stuff goes here1</span>
    
                <a href="#" id="app2-link">link2</a>
                <span class="socialApp" id="app2">stuff goes here2</span>
    
                <a href="#" id="app3-link">link2</a>
                <span class="socialApp" id="app3">stuff goes here3</span>            
            </aside>
        </body>
    </html>​​​​​​​​​​​
    

    If you’re keen to learn more about JavaScript development then may I suggest reading Object Orientation JavaScript which provides an excellent introduction to the language and some of it’s quirks.

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

Sidebar

Related Questions

I'm trying to get a brand new skeleton app running in rails 3.1.0.rc8. However,
I am brand new to jQuery (and really javascript in general) and am trying
This seems really simple but Im brand new to JavaScript. I have a link
I'm brand spankin' new to Javascript. Here's what I want to do. I want
Brand new to Cocoa and I'm trying to figure out how to copy an
I'm brand new at Python and I'm trying to write an extension to an
I'm brand new to SQL Server 2008, and have some newbie questions about the
I'm brand new to jQuery (and javascript in general). I've been meaning to use
First I'm brand new to JS but have an idea that object classes are
i am using asp.net framework 4 and i have created brand new project and

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.