Try to match id instead of whole object like,

$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});

// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var modal = document.getElementById('myModal');

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
/*window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}*/
$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

Answer from Rohan Kumar on Stack Overflow
🌐
W3Schools Blog
w3schools.blog › home › jquery ui dialog
jQuery UI Dialog - w3schools.blog
December 24, 2019 - In the above example, we are displaying the usage and the behavior of the options buttons, title, and position in the dialog widget. ... <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Dialog functionality</title> <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-widget-header,.ui-state-default, ui-button{ background:crimson; border: 2px solid brown; color: white; font-weight: bold; } </style> <!-- Javascript --> <script> $(function() { ( "#opener" ).click(function() { $( "#dialg" ).dialog( "open" ); }); }); </script> </head> <body> <!-- HTML --> <div id="dialg" title="Definition: Knowledge">"The theoretical or practical understanding of a subject."
🌐
jQuery UI
jqueryui.com › dialog
Dialog | jQuery UI
The basic dialog window is an overlay positioned within the viewport and is protected from page content (like select elements) shining through with an iframe.
🌐
W3Schools
w3schools.com › jquerymobile › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
TutorialsPoint
tutorialspoint.com › jqueryui › jqueryui_dialog.htm
JqueryUI - Dialog
Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output − · The following example demonstrates the use of resize event method. <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Dialog functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS
🌐
Raymond Camden
raymondcamden.com › 2009 › 02 › 01 › Creating-a-Dialog-with-jQuery-UI
Creating a Dialog with jQuery UI
February 1, 2009 - For my example code I took both folders and copied them to my demo: Alright, so next we will create a very simple html page. I based my code on the dialog documentation page with one big change. Their demo code runs the dialog immediately when the page loads. I think in almost all cases folks will want to show a dialog based on some event. Here is what I came up with: <html> <head> <script src="js/jquery.js"></script> <script src="js/ui/ui.core.js"></script> <script src="js/ui/ui.dialog.js"></script> <script> function showDialog(){ $("#example").dialog(); return false; } </script>
Top answer
1 of 2
8

Try to match id instead of whole object like,

$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});

// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var modal = document.getElementById('myModal');

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
/*window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}*/
$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

2 of 2
1

please i am getting answer by using function

$(window).on('click', function(event){
if(event.target.nodeName != 'BUTTON'
&& $('#myModal').css('display') == "block"){
    $('#myModal').css({display: "none"});
}
});
🌐
W3Schools
w3schools.com › jsref › dom_obj_dialog.asp
HTML DOM Dialog Object
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
W3Schools
w3schools.com › jsref › met_dialog_show.asp
HTML DOM Dialog show() Method
If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial · HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
Find elsewhere
🌐
Jquerymodal
jquerymodal.com
jQuery Modal
This example demonstrates how to add extra classes to the close button (for custom styles for the close button):
🌐
W3Schools
w3schools.com › howto › howto_css_modals.asp
W3Schools.com
Create a Website Make a Website Make a Static Website Host a Static Website Make a Website (W3.CSS) Make a Website (BS3) Make a Website (BS4) Make a Website (BS5) Create and View a Website Create a Link Tree Website Create a Portfolio Create a Resume Make a Restaurant Website Make a Business Website Make a WebBook Center Website Contact Section About Page Big Header Example Website · 2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid List Grid View Mixed Column Layout Column Cards Zig Zag Layout Blog Layout · Google Charts Google Fonts Google Font Pairings Google Set up Analytics · Convert Weight Convert Temperature Convert Length Convert Speed · Get a Developer Job Become a Front-End Dev. Hire Developers ... Learn how to create a Modal Box with CSS and JavaScript. A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal
🌐
jQuery Script
jqueryscript.net › demo › Simple-jQuery-Modal-Dialog-Box-Plugin-Dialog
jQuery Dialog Examples
Download This Plugin · Back To jQueryScript.Net · jQuery Dialog Examples
🌐
Bellevue-lots
bellevue-lots.com › wp-content › uploads › 2021 › 04 › tungstona-business-fda › jquery-dialog-w3schools.html
Jquery dialog w3schools
Hasta ahora hemos trabajado con ... the jQuery JavaScript Library. Examples might be simplified to improve reading and basic understanding. link How jQuery Works 101: jQuery Object Methods. The showModal() method shows the dialog. W3Schools is optimized for learning, testing, ...
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › jsref › dom_obj_dialog.asp.html
HTML DOM Dialog Object
The Dialog object also supports the standard properties and events. ... Color Converter Google Maps Animated Buttons Modal Boxes Modal Images Tooltips Loaders JS Animations Progress Bars Dropdowns Slideshow Side Navigation HTML Includes Color Palettes Code Coloring ... Your message has been sent to W3Schools. HTML Tutorial CSS Tutorial JavaScript Tutorial W3.CSS Tutorial Bootstrap Tutorial SQL Tutorial PHP Tutorial jQuery ...
🌐
W3Schools
w3schools.com › jsref › met_dialog_showmodal.asp
HTML DOM Dialog showModal() Method
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
🌐
CodePen
codepen.io › Kergos › pen › AeGKZR
JQuery Dialog example
$(document).ready(function() { $('#dialog').dialog({ autoOpen: false }); $('#openDialog').click(function() { $('#dialog').dialog('open'); }); });
🌐
O'Reilly
oreilly.com › library › view › jquery-ui › 9781449325176 › ch04.html
4. Dialog Boxes - jQuery UI [Book]
March 17, 2012 - This dialog box includes text content and a title bar that contains a close button. Users can move the box on the page and resize it. jQuery UI requires us to use the following conventions: A global <div> block surrounds the whole with a title ...
Author   Eric Sarrion
Published   2012
Pages   244
🌐
W3Schools Blog
w3schools.blog › home › jquery ui tutorial
jQuery UI Tutorial - w3schools.blog
January 28, 2020 - jQuery UI Tutorial for beginners with example program code on Introduction, Features, Draggable, Droppable, resizable, SelectablejQueryUI Sortable, Accordion, Autocomplete, Button, Datepicker, Dialog, Menu, Progressbar, Selectmenu, Slider, spinner, Tabs, Tooltip, show, hide, toggle, removeClass, Switch Class, Animation, Easing, addClass, Effect and more.
🌐
W3Schools
w3schools.com › jquery › default.asp
jQuery Tutorial
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.