onkeypress on a <a> tag

thormayer picture thormayer · Mar 11, 2012 · Viewed 16.4k times · Source

I'm trying get my <a> tag triggered when the user press on the "enter" key. (onkeypress).

my <a> tag:

<a href="javascript:search()" onkeypress="return runScript(event)">

this is my javascript :

 function runScript(e)
   {
        if (e.keyCode == 13) {
                alert("dssd");
        return false;

      }
   }

I dont know whats messed up ?

Answer

Jignesh Rajput picture Jignesh Rajput · Mar 11, 2012

its work for me

 <a href="javascript:search();"  onkeypress="return runScript(event);">Open in new window using javascript</a>

javaScript

 window.runScript = function (e) {

       if (e.keyCode == 13) {
           alert('ss');
           return false;
       }
       else {

           return true;
       }
   }

   window.search = function () {
       alert('s');
   }

live demo : fiddle