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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:08:22+00:00 2026-06-06T09:08:22+00:00

I want to let a div show and disappaer with jquery and css. The

  • 0

I want to let a div show and disappaer with jquery and css.
The code does not work:

HTML

<html>
    <head>

    </head>
    <body>
        <div class="box" id="b1">
            <a href="#">click to show content</a>

            <div class="content">
                here is content here is content here is content
                <div class="button" id="b2">remove content</div>
            </div
    </body>
</html>

jQuery

$(document).ready(function() {
    $('#b1').click(function() {

        $('.content').css({
            "display": "block"
        });
    });

    $('#b2').click(function() {

        $('.content').css({
            "display": "none"
        });
    });
});

Demo: http://jsfiddle.net/jTgRF/41/

  • 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-06T09:08:25+00:00Added an answer on June 6, 2026 at 9:08 am

    The problem is that you don’t stop the event from propagating.

    When you click the button it actually hide the element at first, but the event propagate up to the parent element, at which you catch the click and show it again. It goes so fast it looks like nothing is happening, but actually when you click on #b2, the elements get hidden, the event then bubble up to the parent #b1, to which you have attached click-event listener to show the content.

    Therefor, you need to use .stopPropagation() on #b2.

    $(document).ready(function(){
         $('#b1').click(function(){
               $('.content').show();
        });
        
        $('#b2').on("click", function(e){ 
               $('.content').hide();
                e.stopPropagation();
        });
    }); 
    

    What you need to do is to pass the click event to the call-backfunction (declared e in my example) and then call stopPropagation() on it to make sure that it doesn’t bubble up to the parent element.

    Here is a working example: http://jsfiddle.net/jTgRF/64/

    Note I use .show() and .hide(), which is exactly the same as what you do with .css(), but is more readable in my opinion.

    Edit:

    As noted by Venu in his answer, the content will show if you click on the black-area as well, not just the "show content"-text. The easiest way to solve that would be to just move the id="b1" attribute from the div with class content and put it on the a-element instead.

    See my updated fiddle: http://jsfiddle.net/jTgRF/65/

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

Sidebar

Related Questions

I have many DIV with the same class name, let save wmplayer. I want
I'll jump to an example. <!DOCTYPE html> <head> <title>Transposing div using CSS</title> <style> div#wrapper
I want to let VSSDB accessed via internet (not LAN) from one machine to
I want to let server to invoke client ActiveX via javascript. The script work
Let's say I have some classes like: <div class=something style=display: none>Some text I would
Let me describe the problem in details: I want to show an absolute positioned
I am using the validate plugin for jQuery and want to show a loading
I have a contact form that I'm trying to show/hide div's and a class
Let's say i have several div's like these: EDIT: <div class=ProfilePic> <a href=#> <img
I'm using TinyMCE to let users edit and format text, output is html code.

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.