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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:40:50+00:00 2026-05-21T08:40:50+00:00

I’m very new with Javascript. I’m trying to do something with Show/Hide functions. html:

  • 0

I’m very new with Javascript.

I’m trying to do something with Show/Hide functions.

html:

<html>
 <head>
  <title> New Document </title>

<style>
#button01 {
 width:100px;
 height:50px;
 margin:10px;
 padding:6px 0 0 0;
 background-color:#f0f0f0;
}

#button01:hover {
 background-color:#ffcccc;
}

#button01 a {
 display:block;
 width:40px;
 height:40px;
 margin:auto;
 background:url("button01.png")
}

#button01 a:hover {
 width:40px;
 height:40px;
 background:url("button01-hover.png")
}

#hidden01 {
 display:none;
 width:300px;
 height:200px;
 margin:0 0 10px 0;
 border:4px solid #ffcccc;
}

#button02 {
 width:100px;
 height:50px;
 margin:10px;
 padding:6px 0 0 0;
 background-color:#f0f0f0;
}

#button02:hover {
 background-color:#cccccc;
}

#button02 a {
 display:block;
 width:40px;
 height:40px;
 margin:auto;
 background:url("button02.png")
}

#button02 a:hover {
 width:40px;
 height:40px;
 background:url("button02-hover.png")
}

#hidden02 {
 display:none;
 width:300px;
 height:200px;
 margin:0 0 10px 0;
 border:4px solid #cccccc;
}
</style>




 </head>

 <body>



<div style="width:300px;">
<div id="button01"><a href="#" onclick="toggle(0);return false"></a></div>


<div id="button02"><a href="#" onclick="toggle(1);return false"></a></div>
</div>

<div id="hidden01">&nbsp;</div>
<div id="hidden02">&nbsp;</div>


 </body>
</html>

script:

        function toggle(offset){
            var i, x;
            var stuff = Array('hidden01', 'hidden02');  //put all the id's of the divs here in order 
            for (i = 0; i < stuff.length; i++){  //hide all the divs
                  x = document.getElementById(stuff[i]);
                  x.style.display = "none";
            }
            // now make the target div visible
            x = document.getElementById(stuff[offset]);
            x.style.display = "block";
         window.onload = function(){toggle(0);}
        }

That’s working, but I want to fix 2 things:

1- Close/Hide hidden divs if I click on it’s corresponding button;

2- After clicking a button, fix hover button image. If click again unfix;

I’ve tried almost all the scripts posted and can not find a solution. I don’t want to open the divs at same time.
If opens one, close the others.

  • 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-21T08:40:51+00:00Added an answer on May 21, 2026 at 8:40 am

    You’re using jQuery, so use jQuery.

    $(function() {
    
        function toggle(offset) {
            $('div[id^=hidden]').hide().eq(offset).show();
        }
    
        toggle(0);
    });
    

    I don’t know what you mean by the two things you want to fix, however. Please clarify.


    Edit

    Okay, I see what you’re going for now. I’ve cleaned up your code a lot.

    HTML

    <div style="width:300px;">
        <div id="button1"><a></a></div>
        <div id="button2"><a></a></div>
    </div>
    
    <div id="hidden1">&nbsp;</div>
    <div id="hidden2">&nbsp;</div>
    

    CSS

    #button1, #button2 {
        width:100px;
        height:50px;
        margin:10px;
        padding:6px 0 0 0;
        background-color:#f0f0f0;
    }
    
    #button1:hover {
        background-color:#fcc;
    }
    
    #button2:hover {
        background-color:#ccc;
    }
    
    #button1 a, #button2 a {
        display:block;
        width:40px;
        height:40px;
        margin:auto;
    }
    
    #button1 a {
        background:url(http://lorempixum.com/40/40?1)
    }
    
    #button2 a {
        background:url(http://lorempixum.com/40/40?3)
    }
    
    #button1 a:hover, #button1.hover a {
        background:url(http://lorempixum.com/40/40?2)
    }
    
    #button2 a:hover, #button2.hover a {
        background:url(http://lorempixum.com/40/40?4)
    }
    
    #hidden1, #hidden2 {
        display:none;
        width:300px;
        height:200px;
        margin:0 0 10px 0;
        border:4px solid #fcc;
    }
    

    JavaScript

    var $buttons = $('div[id^=button]'),
        $hiddenDivs = $('div[id^=hidden]'),
        HOVER_CLASS = 'hover';
    
    $buttons.live('click', function() {
        var $this = $(this),
            i = $this.index();
    
        $buttons.removeClass(HOVER_CLASS);
        $this.addClass(HOVER_CLASS);
        $hiddenDivs.hide().eq(i).show();
    }).first().click();
    

    Demo


    Last edit

    Changed JavaScript and CSS. http://jsfiddle.net/mattball/bNCNQ/

    CSS

    #button1:hover a, #button1.hover a {
        background:url(...)
    }
    
    #button2:hover a, #button2.hover a {
        background:url(...)
    }
    

    JS

    $buttons.live('click', function () {
        var $this = $(this),
            i = $this.index(),
            show = !$this.hasClass(HOVER_CLASS);
    
        $buttons.removeClass(HOVER_CLASS);
        $this.toggleClass(HOVER_CLASS, show);
        $hiddenDivs.hide().eq(i).toggle(show);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.