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."
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
Raymond Camden
raymondcamden.com โ€บ 2009 โ€บ 02 โ€บ 01 โ€บ Creating-a-Dialog-with-jQuery-UI
Creating a Dialog with jQuery UI
February 1, 2009 - I seem to remember having similar issues with the core jQuery library as well and I'm over that as well, so maybe it's simply something that I have to get used to. I'll leave off with one more quick tip from Richard Worth. Remember my trouble with hiding the div on page load? He pointed out that you can also dynamically create dialogs with strings: $("<div>I'm in a dialog</div>").dialog()
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 โ€บ met_dialog_show.asp
HTML DOM Dialog show() 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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ jqueryui โ€บ jqueryui_dialog.htm
JqueryUI - Dialog
The dialog (action, params) method can perform an action on the dialog box, such as closing the box. The action is specified as a string in the first argument and optionally, one or more params can be provided based on the given action.
๐ŸŒ
Bellevue-lots
bellevue-lots.com โ€บ wp-content โ€บ uploads โ€บ 2021 โ€บ 04 โ€บ tungstona-business-fda โ€บ jquery-dialog-w3schools.html
Jquery dialog w3schools
At W3Schools you will find a complete reference of all jQuery selectors, methods, properties and events. 4 and will be removed in 1. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ dom_obj_dialog.asp
HTML DOM Dialog Object
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_css_modals.asp
W3Schools.com
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_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.
๐ŸŒ
Jquerymodal
jquerymodal.com
jQuery Modal
jQuery Modal is the easiest way to display modal windows with jQuery. Built by Kyle Fox.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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 ...