Search code examples
javascriptcsshtmlmodal-dialogonkeypress

onkeypress event on modalDialog


i have this code:

<a href="#openModal">Open Modal</a>
<div id="openModal" class="modalDialog">
<div>
    <a href="#close" title="Close" class="close">X</a>
    <h2>Modal Box</h2>
    <input type="radio" value="rdRec" checked="checked" />Rec
    <input type="radio" value="rdPot" />Pot
    <input type="button" id="btnOk" value="ok" onkeypress="checkey();">
</div>

<script type="text/javascript">
  function checkey()
  {
     var code = window.event.keyCode;
     if(code == 13) 
     {      
     document.getElementById('btnOk').click();
     return false;
     }
  }
</script>

All i want is when the popup is open and the radio button Rec is select/checked i want to click on enter key and automatically trigger the btnOk...


Solution

  • I think the answer you're looking for is in this previously answered question: Use Javascript to change which submit is activated on enter key press

    document.onkeypress = processKey;
    
    function processKey(e)
    {
        if (null == e)
            e = window.event ;
        if (e.keyCode == 13)  {
            document.getElementById("btnOk").click();
        }
    }