Search code examples
javascriptjqueryhtmlwindow

Window is supposed to close on click, instead closes immediately


This window is supposed to close when the user clicks the "close" link. Instead it closes immediately upon opening. What's the deal?

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<style>
img {
    display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
p {
    text-align: center;
}
</style>

 <script 
 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
 </script>
</head>
<body>
<img src = "assets/images/trophy.png"></img>
<p>YAY!  U R TEH WINZ!</p>
<p><a href = "#" class = "exit">Close.</a></p>
<script>
$(".exit").onClick(this.window.close());    
</script>
</body> 
</html>

<!-- UGH, what is going on?  Either clicking takes you nowhere, or it happens 
right on load! -->

Solution

  • There is no onClick event in jQuery. So you will be getting following error in your console:

    Uncaught TypeError: $(...).onClick is not a function
    

    jQuery .click() event - Click Here

    Your event should be as follows:

    $(".exit").click(function(){
         window.close();
    });