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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:50:30+00:00 2026-05-16T02:50:30+00:00

I’m just starting to learn Javascript; I do however have experience in programming using

  • 0

I’m just starting to learn Javascript; I do however have experience in programming using the C family of languages.

I’m basically trying to get a grasp on how the getElementById() function families work. To do this I’m trying to create a basic show/hide panel.

<div>
   <div id="box1">
      Title 
      <span class="time">Time</span>
      <span class="else" style="display:none;">Something Else</span>
      <a class="icon" href="#" OnClick="javascript:toggle('box1');">[-]</a>
      <div class="content">Content</div>
   </div>
</div>

Here is what I have so far for code:

function toggle(whichLayer)
    {
        var elem, vis;

        if (document.getElementById) // this is the way the standards work
        {
            elem = document.getElementById(whichLayer);
        }

        vis = elem.style;

        // if the style.display value is blank we try to figure it out here
        if (vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        {
            vis.display = (elem.offsetWidth != 0 && elem.offsetHeight != 0) ? 'block' : 'none';
        }

        vis.display = (vis.display == '' || vis.display == 'block') ? 'none' : 'block';       
    }

It currently just shows and hides the box1 div which is all the code is supposed to do. Here is what I would like to be able to do:

  1. I’d like to be able to toggle the [+] on click to [-] and vis versa.
  2. I’d like to be able to hide div.content
  3. I’d like to be able to toggle of the span from time visible to the other one and vis versa.

I can think about how to do this in pseudo code, but I don’t know javascript well enough.

OnClick(string divId)
{
   if (divId.Hide)
   { 
      divId.span.else.display = block;
      divId.span.time.display = none;

      divId.a.icon.text = "[+]";

      divId.div.content.display = none;
   }
   else //Show
   {
      divId.span.else.display = none;
      divId.span.time.display = block;

      divId.a.icon.text = "[-]";

      divId.div.content.display = block;
   }
}

I guess the part that I’m stuck on is where I select the classes inside of the id=”box1″ div. I don’t know how to interate through them by class name. I’m found stuff using google on how to do a getElementByClass() implementation because JS doesn’t have one, but its over my head.

Any help would be greatly appreciated.

  • 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-16T02:50:30+00:00Added an answer on May 16, 2026 at 2:50 am

    Most recent browsers support document.getElementsByClassName. IE8 does not. (http://www.quirksmode.org/dom/w3c_core.html#t11) Most JavaScript libraries do offer a simple way to select elements by class, but they each have their own pros and cons.

    If you don’t want to use a JavaScript library (Which is fine; there are great reasons for not using additional libraries.), you could do something like this:

    elems = document.getElementsByTagName("h2");
    
    for ( i=0; i<elems.length; i++ )
    {
    if ( elems[i].className == "myAwesomeClass" )
       { //  Do whatever stuff needs to happen to the class
         elems[i].style.color="red"; 
         }
    }
    

    In the above code, I’ve assumed that each element with the desired class has the same tag. If necessary, you could do document.getElementsByTagName(“*”) to select all tags, but that would probably take longer for the browser to process.

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

Sidebar

Ask A Question

Stats

  • Questions 479k
  • Answers 479k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want people to be able to share the… May 16, 2026 at 5:52 am
  • Editorial Team
    Editorial Team added an answer I "include" the configuration file so that it's visible in… May 16, 2026 at 5:52 am
  • Editorial Team
    Editorial Team added an answer Window > Preferences > Java > Editor > Content Assist… May 16, 2026 at 5:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.