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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:22:13+00:00 2026-06-12T21:22:13+00:00

I have a button which, when clicked, changes the visibility of a div as

  • 0

I have a button which, when clicked, changes the visibility of a div as such:

<button type="button" id="editbutton" onclick="plus()" alt="Edit"></button>

function plus() {
var f = document.getElementById('edit');
if (f.style.visibility == 'hidden') {
   f.style.visibility = 'visible';
   }
else
    f.style.visibility = 'hidden';
}

This works fine. But now I want to add the functionality so that if the user clicks anywhere outside of the div, the div will go back to being hidden.

Initially I tried my own methods, but was unsuccessful because whenever I click the button to make the div visible, it is made hidden again by the new code. I’ve looked into some of the jQuery examples others have posted, however none of the ones I have tried fix this problem. Any help would me much appreciated.

EDIT: To clarify, the issue is that because the div is initially made visible by the user clicking on a button, whenever I click the button, the div is made visible but is immediately hidden again because that counts as clicking on the body of the page.

  • 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-12T21:22:14+00:00Added an answer on June 12, 2026 at 9:22 pm

    You could call ‘e.stopPropagation()’ on the event from within your click event handler when a user clicks within your div – this will stop the event from ‘bubbling’ up the DOM.

    And then if you attach an event handler to the BODY element listening for clicks, you can use that event handler to hide the DIV; knowing that a click on the DIV itself won’t bubble the event up to the BODY handler.

    In this way any click on another element, unless the handler also calls stopPropagation(), will get caught by your handler. So if there are other cases where you don’t want the div to hide you can stop it happening by calling stopPropagation() as required.

    E.g.:

    $("#edit-button").click( function(e) {
        $("#edit").toggle();
        e.stopPropagation();
    });
    
    $('#edit').click(function(e) {
        e.stopPropagation();
    });
    
    $("body").click( function(e) {
        $("#edit").hide();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button which creates a div. Once clicked I would like to
I have this button, and when clicked, it sends an ajax call which changes
I have a Button which when clicked will execute an addQty method and in
On my website, I have a button which when clicked displays a feedback form
I have a DDL and Button which when clicked create a dynamic table; all
I want to create a button which when clicked calls an action. I have
I have a button which creates form elements on click . My addelement function
I have a single form that, depending on which radio button is clicked (Login
i have a div which when clicked has another div underneath it slide up
I have button which post values of form to url but during post i

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.