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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:13:26+00:00 2026-05-12T15:13:26+00:00

I have a popup div and want to hide it when users click outside

  • 0

I have a popup div and want to hide it when users click outside of the div. Inside the div, I have a checkbox…

Problem is, the div’s onblur event gets fired when trying to click on the checkbox. I put cancelEventBubble code on the onclick of the checkbox but the onblur fires before the checkbox onclick event…

any ideas or suggestions? This seems so simple to do but is not quite that simple…

Regards,
Albert

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
<style type="text/css">
.popup
{
    display: none;
    position: relative;    
    border: solid 1px black;
    width: 100px;
    left: 100px;
    top: 100px;
}

.lookupButton
{
    width: 15px;
    height: 20px;
    background-color: blue;
}

.lookup
{
    border: solid 1px green;
}
</style>
<script language="javascript" type="text/javascript">
var popupVisible = false;

function cancelEventBubble(e) {
     if (!e) var e = window.event;
     e.cancelBubble = true;
     if (e.stopPropagation) e.stopPropagation();
}

function showPopup(obj)
{
    if (!popupVisible)
    {
        popupVisible = true;
        document.getElementById("popup").style.display = "block";
        document.getElementById("popup").focus();
    }
    else
    {
        popupVisible = false;
        document.getElementById("popup").style.display = "";
    }
}

function hidePopup()
{
    popupVisible = false;
    document.getElementById("popup").style.display = "";
}

function setTextValue(obj)
{
    var combo = document.getElementById("cbo1");
    if (combo.value.indexOf(obj.value) == -1)
    {
        combo.value += obj.value + "; ";
    }
    else
    {
        combo.value = combo.value.replace(obj.value + "; ", "");
    }
}

</script>
</head>
<body>
<table><tr><td>
    <input readonly="readonly" type="text" name="cbo1" id="cbo1" />
</td><td>
    <div class="lookupButton" onclick="showPopup(this);"></div>
</td></tr></table>

<div id="popup" class="popup" onblur="hidePopup()">
    <input id="chk0" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="John" />John<br />
    <input id="chk1" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Jack" />Jack<br />
    <input id="chk2" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="James" />James<br />
    <input id="chk3" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Richard" />Richard<br />
    <input id="chk4" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Tim" />Tim<br />
</div>
</body>
</html>
  • 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-12T15:13:26+00:00Added an answer on May 12, 2026 at 3:13 pm

    On modern browsers the onblur event doesn’t fires with div elements, a crossbrowser approach that can also deal with the issues of IE could be to use event delegation, binding a click event handler to the document, and hide the popup when the clicked element is not a checkbox or the lookupButton, for example:

    document.onclick = function (e) { 
      e = e || window.event; 
      var element = e.target || e.srcElement; 
    
      if (element.tagName != "INPUT" && element.type != "checkbox" &&
          element.className != "lookupButton") { 
        hidePopup(); 
      } 
    }; 
    

    Check the above example with the rest of your code here.

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

Sidebar

Related Questions

I have a div that I want to write to a popup window (for
I'm writing a plugin to popup a div on a click. I have a
i have a div with class='divclass' and some text inside it... now i want
I have a modal popup that initially shows some content but expands a div
Hey i have this div that shows as a popup: <div class=popup> </div> Then
I have a scrollable area that contains popup menus. Conceptually, something like this: <div
hi i have following code in this i have div card-208 on click i
The Scenario: I have an asp.net website where I show a div popup on
I have four divs on a popup and each click makes a ajax call
i am running into a problem that i have a jquery modal popup in

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.