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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:50:36+00:00 2026-06-18T09:50:36+00:00

I don´t work much with javascript and I googled a lot the last few

  • 0

I don´t work much with javascript and I googled a lot the last few hours to figure out my problem. I guess I just don´t know the right words to search for.

This is what I want to achieve. A list of Links with a small arrow next to it. When you click on a link, a hidden div is shown and the arrow should move down to show that the text is “open”. When I click on another text, the arrow should move up again and the div should be hidden. The other div would open accordingly. But the code is still a bit buggy. When I click on a link it gets the class “expanded” – but this class is not being automatically removed when I click on another item.

Here is my code so far… It doesnt work at all on jsfiddle, don´t know why http://jsfiddle.net/DvH75/

var _hidediv = null
   function showdiv(id) {
   if(_hidediv)
      _hidediv();
   var div = document.getElementById(id);
   div.style.display = 'block';
    $(".toggle").on("click", function(){
        $(this).toggleClass("expanded");
      });
   _hidediv = function () { div.style.display = 'none'; };
   }

And this is an example of what I want to archieve. The only difference is that I want an open item to close once another is opened:
https://www.facebook.com/help/473865172623776/

I hope you understand my question and can help me
Thank you very much!!

  • 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-18T09:50:37+00:00Added an answer on June 18, 2026 at 9:50 am

    Your toggleClass() call is adding the class to the one clicked, but I don’t see you removing the class of the previous clicked. What I would do is something like:

    $('.expanded').each(function() {
        $(this).removeClass('expanded');
    });
    

    EDIT: Working Example

    I spent some time in jsFiddle and got this working example (warning, jsFiddler doesn’t seem to work in IE8, so use FireFox or Chrome to view the example).

    First, I dropped the ‘display: none;‘ from the UL.fade_text li div style in main.css as it’s not needed any longer.

    Next, I added class markers for all the *div*s. I could have probably done something else, but this was a quick-n-dirty example.

    Last, I re-wrote the JS to be more jQuery-ish. I made a function that will show or hide the divs based on if it finds an anchor tag with the class toggle that also has the class expanded. Then, I added a click event handler to all the anchor tags, again using jQuery, that calls the showHideDivs() function. I also added a call to showHideDivs() in the $(document).ready() so that the initial state is set properly.

    Here is the new JS code. It could probably be tweeked to optimize things a bit, but it works like I would expect:

    $(document).ready(function () {
        function showHideDivs() {
            $('.showHideDiv').each(function () {
                if ($(this).prevAll('a.toggle:first').hasClass('expanded')) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        }
    
        $('a.toggle').click(function () {
            var addExpanded = !$(this).hasClass('expanded');
            $('a.toggle').removeClass('expanded');
            if(addExpanded) {
                $(this).addClass('expanded');
            }
            showHideDivs();
        });
    
        showHideDivs();
    });
    

    I think the benefits of this approach is that it will gracefully degrade for browsers that have issues with the JS or just have JS turned off. The end result is that those people would see all the div tags expanded.

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

Sidebar

Related Questions

As a novice, I really don't know much about javascript. Kindly help me in
I don't know much about Javascript, and this function I wrote doesn't seem to
Arg. Inheriting projects is SO much fun. Especially when they don't work well, and
Hello, I do not know very much about javascript, not much at all to
I don't have much experience with Javascript, Node.js or Socket.io. I'm trying to create
The following code don't work- TextView myLocation = new TextView(this); myLocation.setText(Sodala, Jaipur, Rajasthan, IN);
Certain list comprehensions don't work properly when I embed IPython 0.10 as per the
Why this code don't work,when i want run this code vwd 2008 express show
can anyone tell me why this code don't work: $q = $_GET['q']; // Load
I'm sure most people ask why things don't work. I am goIng to mix

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.