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

  • Home
  • SEARCH
  • 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 628145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:36:18+00:00 2026-05-13T19:36:18+00:00

I have little piece of javascript to show/hide divs based on which link is

  • 0

I have little piece of javascript to show/hide divs based on which link is clicked in a list. Its not very eligant, but it works fine. What I’d like to do is to assign an active state the a list item depending on which div is showing. Here is my JS and HTML:

var ids=new Array('section1','section2','section3','section4');

function switchid(id){  
    hideallids();
    showdiv(id);
}

function hideallids(){
    //loop through the array and hide each element by id
    for (var i=0;i<ids.length;i++){
        hidediv(ids[i]);
    }         
}

function hidediv(id) {
    //safe function to hide an element with a specified id
        document.getElementById(id).style.display = 'none';
}

function showdiv(id) {
    //safe function to show an element with a specified id        
        document.getElementById(id).style.display = 'block';
}

html:

<ul>
<li class="activeItem"><a href="javascript:switchid('section1');">One</a></li>
<li><a href="javascript:switchid('section2');">Two</a></li>
<li><a href="javascript:switchid('section3');">Three</a></li>
<li><a href="javascript:switchid('section4');">Four</a></li>
</ul>

<div id="section1" style="display:block;">1111111</div>
<div id="section2" style="display:none;">2222222</div>
<div id="section3" style="display:none;">3333333</div>
<div id="section4" style="display:none;">4444444</div>

When section 2 (or whichever) is clicked, I’d like the class “activeItem” to be removed from the li it is currently on and applied to the current li. Is this possible with javascript? I think it is, but I can’t figure out how to implement it client side.

Thanks!

  • 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-05-13T19:36:18+00:00Added an answer on May 13, 2026 at 7:36 pm

    Change your anchors to use the onclick event instead of the href javascript code.

    <a onclick="switchid('section1');return false;">One</a>
    

    Then pass the argument this into your switchid function. this is a javascript keyword which in this scenario refers to the element (a).

    <a onclick="switchid('section1', this);return false;">One</a>
    

    Now to make the switchid function modify the list.

    function switchid(id, el){  
        hideallids();
        showdiv(id);
    
        // rejiggered for text nodes
        var li = el.parentNode.parentNode.childNodes[0];
        while (li) {
            if (!li.tagName || li.tagName.toLowerCase() != "li")
                li = li.nextSibling; // skip the text node
            if (li) {
                li.className = "";
                li = li.nextSibling;
            }
        }
    
        el.parentNode.className = "activeItem";
    }
    

    Try it out: http://jsbin.com/esazu3/edit

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

Sidebar

Related Questions

I have this very usefull little piece of javascript that centers mig div. By
I have this little piece of jQuery/jqModal code which works fine. However, I am
I have the following piece of code which is not running as I expected:
I have a little piece of code which populates a CellTable from a Type
I wanted to give my users a little piece of JavaScript or HTML code
I have this little piece of code : private Dictionary<string, IList<KeyValuePair<int, string>>> EnumsCollection =
I have this little piece of code: <script> $(window).bind('beforeunload', function() { $.ajax({ async: false,
I have this little piece of code, and I'm trying to convert a JSON
For whatever reason, this little piece of dojo isn't working. I have to admit
I have a little piece of code in C++: #include <iostream> #include <iterator> #include

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.