Click here to Skip to main content
15,886,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have AJAX modal popup. I want it to be closed on pressing ESC key.

XML
<script type="text/javascript">
        document.onkeyup = Esc;
        function Esc() {
            var KeyID = event.keyCode;
            if (KeyID == 27) {
                if ($find("mpForLeave")) {
                    $find("mpForLeave").hide();
                }
                
            }
        }


So I have written this script. But I am unable to understand where I should call this?
Please help.
Posted
Updated 6-Aug-13 20:25pm
v2
Comments
Ankur\m/ 7-Aug-13 2:27am    
You can write it on jQuery document ready event.

Use below in your page like:-

XML
<script type="text/javascript">
$(document).keyup(function(e) {
    if (e.keyCode == 27)
    {
       $find("mpForLeave").hide();
    }
});
</script>
 
Share this answer
 
Comments
Torakami 7-Aug-13 2:53am    
i tried this but not working .. may be because my modal popup is inside update panel .. suggest
Torakami 7-Aug-13 2:53am    
Do i need to call this function somewhere else as well ??
TrushnaK 7-Aug-13 3:09am    
try to use in comman.js file and add reference of that js to page.
document.onkeydown = overrideKeyboardEvent;

JavaScript
function overrideKeyboardEvent(e){
if(e==null)
{
    e=window.event;
}
  switch(e.type){

PHP
if (e.keyCode == 27)
   {
      $find("mpForLeave").hide();

// now you are getting here error
instead you can use $("#uridofelement").hide();
   }
}
}


regards ...:)
 
Share this answer
 
v2
Comments
Torakami 7-Aug-13 3:10am    
why switch .. n where are we checking cases .. not proper i guess
Dholakiya Ankit 7-Aug-13 3:14am    
it proper e.type means event and switching for if you want to use different keys then you can add here torakami regards ..:)
Torakami 7-Aug-13 3:18am    
its giving me an error can not set hide to null . that means he is unable to find control .. because of update panel i guess
Dholakiya Ankit 7-Aug-13 3:19am    
give me ur source code of html
Dholakiya Ankit 7-Aug-13 3:20am    
check updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900