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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:52:09+00:00 2026-05-24T10:52:09+00:00

Please bear with me here, there’s probably a simple answer… I need to make

  • 0

Please bear with me here, there’s probably a simple answer…

I need to make a tabbed layout section with the simplest possible css and javascript.

I have some html divs arranged on top of each other using css, and initially have them all set to ‘visibility:hidden’. Here is the html…

<div id="tabs1" class="hide">
<div id="tabs2" class="hide">
<div id="tabs3" class="hide">

What I want to do is write some javascript to change the visibility property of one div to ‘visible’ when a link is clicked.

relevant CSS for this is just:

.hide { visibility: hidden; }
.show { visibility: visible; }

The following javascript works to change from hidden to visible by changing the class property –

function showThisTab(id)
{
// show this
document.getElementById(id).className += " show";
}

Using the following html links to call the function with onclick:

<a href="#" onclick="javascript:showThisTab('tabs1')">Tab 1</a>
<a href="#" onclick="javascript:showThisTab('tabs2')">Tab 2</a>
<a href="#" onclick="javascript:showThisTab('tabs3')">Tab 3</a>

All of the above works as it should, but obviously if you click more than one link, all the tabs end up being visible at once on top of each other. So I also need to reset all other tabs to hidden each time, leaving just the one the user clicked visible.

I rewrote my js to the following:

<script type="text/javascript">
/*<![CDATA[*/

function showThisTab(id)
{
var i = 1;

// hide all other tabs:
while(i<10) {
    var divID = 'tabs' + i;
    if (divID !== null) {
    document.getElementById(divID).className += " hide";
    }
    i++;
    }

// show this one
document.getElementById(id).className += " show";
}
/*]]>*/
</script>

This should iterate through the divs labelled tab1 to tab10 appending ‘hide’ to the class property (should work right?). After that, append ‘show’ to the one I want to show as normal.

However, the script just breaks, google chrome shows the following error: “Uncaught TypeError: Cannot read property ‘className’ of null”

This happens when it reaches the line:
document.getElementById(divID).className += ” hide”;

So by deduction it appears divID is somehow still null at this stage even with the if statement? I’m stuck here anyway, and it’s frustrating because I’m sure I’m close.

Anyone able to point me in the right direction?

tl;dr – Above code gives “Uncaught TypeError: Cannot read property ‘className’ of null” and I dunno why.


EDIT: @RichieHindle thanks for pointing out this fixes the error (I was being dumb as suspected)

I was also being dumb about the appending the classname, the working code is:

<script type="text/javascript">
/*<![CDATA[*/
function showThisTab(id)
{
var i = 1;
// hide all other tabs:
while(i<10) {
    var divID = 'tabs' + i;
    var div = document.getElementById(divID);
    if (div !== null) {
        div.className = " hide";
    }
    i++;
}
// show this one
document.getElementById(id).className = " show";
}
/*]]>*/
</script>
  • 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-24T10:52:10+00:00Added an answer on May 24, 2026 at 10:52 am

    Did you mean:

    var div = document.getElementById(divID);
    if (div !== null) {
        div.className += " hide";
    

    Your test:

    if (divID !== null) 
    

    will always succeed, even if no such div appears in the document.

    Unless you have at least 10 tabs, your code will break when i exceeds the number of tabs.

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

Sidebar

Related Questions

I am very new to Perl, so please bear with my simple question: Here
Ok, please bear with my noob question here. I'm doing the simple task of
I am not an expert web-dev so please bear with me here. I would
A total newbie question, so please bear with me here ;) When a key
everyone. I'm quite new here so please be tolerant if I make any mistakes.
I am still learning the whole WCF thing, so please bear with me here.
I am both new here and in IOS programming, so please bear with my
Total newbie here, please bear with me. Building a very small personal app that
This is my first question here so please bear with me - I apologise
First post here, so please bear with me if I get something wrong. I'm

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.