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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:27:38+00:00 2026-06-10T22:27:38+00:00

I have a div to show/hide on click and this div will load content

  • 0

I have a div to show/hide on click and this div will load content from me.html via ajax load. I am adding a class class added to this a tag and then when I click again on a tag with added class, it should hide the div. But this doesn’t work.

$(function() {
    $("#nav a").click(function() { 
        $(this).toggleClass('added');
        if($(".o1").length <= 0) { 
            $("body").append('<div class="o1"></div>');
            $(".o1").load("me.html").css("display","block");
        }
        $(".added").click(function() { 
            $(".o1").css("display","none");
        })
    });
});

Any ideas?

  • 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-10T22:27:39+00:00Added an answer on June 10, 2026 at 10:27 pm

    Try this. This will setup the object if it doesn’t exist. Then once it does it will toggle it if for future clicks.

    $(function() {
        $("#nav a").click(function() { 
            $(this).toggleClass('added');
            if($(".o1").length <= 0)
            { 
                $("body").append('<div class="o1"></div>');
                $(".o1").load("me.html");
            } else if($(this).hasClass("added"))
                $(".o1").hide();
            } else {
                $(".o1").show();
            }
        });
    });
    

    Adding a click handler to all .added links with your code would create a problem, because in your code you have.

    This shows the timeline of what is happening. Hope you can follow. The numbers in brackets are events. The same number signifies they happen at the same time.

    (1) #nav a is clicked then #nav a gets a class of added then .o1 is added then any link with added class gets a click event

    (2) #nav a is clicked then #nav a removes class of added then .o1 already exists then any link with added class gets a click event (but not this link, but it still has the previous click event, because it wasn’t dettached)

    (2) .added is also considered clicked at the same time as action above because it was attached during (1)

    (3) #nav a is clicked then #nav a gets a class of added then .o1 already exists then any link with added class gets a click event

    (3) .added is also considered clicked at the same time as action above because it was attached during (1)

    (4) #nav a is clicked then #nav a gets a class of added then .o1 already exists then any link with added class gets a click event

    (4) .added is also considered clicked at the same time as action above because it was attached during (1)
    (4) .added is also considered clicked at the same time as action above because it was attached during (3)

    etc …

    The key thing to grasp is you are not detaching click events, instead you are attaching more and more.

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

Sidebar

Related Questions

I have a div show hide function which is working fine. when I click
I have this jquery script which suppose to hide/show div element base on the
I have the following HTML.. I'd like to hide the SIDEBAR div on load
I have a contact form that I'm trying to show/hide div's and a class
The code below will show / hide a div on click. It works perfectly
I have the following HTML code <div class=text>bla bla bla bla</div> <div class=button>Show</div> And
I have 3 radio buttons which hide/show some div's. The radio buttons are getting
I have done a hide and show of a div element, on clicking a
i have any div for show details of project ( class main ). now
I have used a javascript to show div by onclick but when i click

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.