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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:28:05+00:00 2026-05-19T09:28:05+00:00

I have 2 divs. I want div id 1 to be visible when the

  • 0

I have 2 divs. I want div id 1 to be visible when the page is loaded and when i click on anywhere in main div id 2 should be visible.when user click again on main 2 should be hidden and 1 visible.`

<div id="main" style="border:1 px dashed red;" >
  <div id="1" style="width:300px;height:50px; ">div One visible</div>
  <div id="2" style="width:300px;height:50px; ">div two visible</div>
</div>

How do i get it done using Jquery? i am not sure how to get the toggling effect

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-19T09:28:05+00:00Added an answer on May 19, 2026 at 9:28 am

    Easy peasy.

    $(function ()
    {
        var $main = $('#main'),
            $1 = $('#1'),
            $2 = $('#2');
        
        $2.hide(); // hide div#2 when the page is loaded
        
        $main.click(function ()
        {
            $1.toggle();
            $2.toggle();
        });
    });
    

    jsfiddle demo →


    Edit

    OP Comment: "Is there anyway i can keep track of which div is visible?"

    There certainly is; it depends on how you want to use it. You can manually maintain a flag:

    $(function ()
    {
        var $main = $('#main'),
            $1 = $('#1'),
            $2 = $('#2'),
            $visible;
        
        $2.hide(); // hide div#2 when the page is loaded
        $visible = $1;
        
        $main.click(function ()
        {
            $1.toggle();
            $2.toggle();
            $visible = ($visible === $1 ? $2 : $1);
        });
    });
    

    Or you can just write a function that figures it out for you:

    $(function ()
    {
        var $main = $('#main'),
            $1 = $('#1'),
            $2 = $('#2');
        
        $2.hide(); // hide div#2 when the page is loaded
        
        $main.click(function ()
        {
            $1.toggle();
            $2.toggle();
        });
        
        
        function whichIsVisible()
        {
            if (!$1.is(':hidden')) return $1;
            if (!$2.is(':hidden')) return $2;
        }
    });
    

    As jAndy points out, this function can be written in a more concise/terse form:

    function whichIsVisible()
    {
        return $1.add($2).filter(':visible');
    }
    

    However, it is semantically different from the first version. The first version returns one of:

    • $1
    • $2
    • undefined

    while jAndy’s version returns one of:

    • $1
    • $2
    • $1.add($2), a two-element jQuery object
    • $(), an empty jQuery object

    So they’re not strictly equivalent.

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

Sidebar

Related Questions

So lets say we have 4 Divs (3 hidden, 1 visible), the user is
I want to have 3 divs aligned inside a container div, something like this:
I want to append a div inside a div which have many divs inside
I have a list of Divs within another div and I want to only
I have a series of <div class=clear-both></div> after a series of divs. I want
If I have two divs, one shown, the other hidden, I want the images
I have several divs on a page. Each div has a heading which I
I have a complex div part on my page which combined with divs as
i have many div's with the same background image and i want change this
I want to display some div's in a wrap. The div have same class

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.