-2

Below is my code:

$(document).keypress(function(e){
  var s = String.fromCharCode(e.which);
  if ( s === "f"){
    alert ("its f");
  }
  else if ( s === "r"){
    alert ("its r");
  }
});

I wanna do this in JavaScript...........!!

How can i do this ?

elclanrs
  • 92,861
  • 21
  • 134
  • 171
EnterJQ
  • 1,014
  • 8
  • 18
  • 5
    Why is the title "Document ready function in native javascript" ? – techfoobar Mar 16 '13 at 06:43
  • 1
    Not sure why he titled it "document ready" to begin with either, but this post would answer that: http://stackoverflow.com/questions/1283445/is-there-a-native-javascript-implementation-of-jquerys-document-ready – Aaron Blenkush Mar 16 '13 at 06:48

4 Answers4

9
document.addEventListener('keypress', function(e) { 
  var key = e.keyCode || e.which;
  ... // rest of code
});
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    Both key event properties are [deprecated](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode), use [keydown](https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event)(pressed) and keyup (released). – Timo Nov 25 '20 at 09:01
2

In native js you can attach events, via onClick, onKeyPress, ... etc element attributes. Do not forget, the event parameter in the event handler function ;) In different browsers, you can read the code of pressed key via different attribute of the event.

Here is an example:

<script type="text/javascript">
  document.getElementsByTagName("body")[0].onkeypress=function(e){
       var s = String.fromCharCode(e.which | evt.witch );
       switch(s){
           case "f":
              alert("key is 'f'");
              break;
           ....
       }
   };
</script>

Or You can use the addEventListener method, like this:

<script type="text/javascript">
     document.getElementsByTagName("body")[0].addEventListener('keypress',function(e){
       var s = String.fromCharCode(e.which | evt.witch );
       switch(s){
           case "f":
              alert("key is 'f'");
              break;
           ....
       }
     });
</script>

You should run these scripts after document loaded. You can subscribe the domready event like this:

<script type="text/javascript">
       document.addEventListener( "DOMContentLoaded", function(){
           //your code here  (one of the above)
       }, false );
</script>

This last section is discussed here: javascript domready?. Unfortunetly you can subscribe domready event different ways depends on the client browser.

A sugggestion (offtopic):

There is a bunch of problems when you use native javascript, because of the browser's uncombatibilities, what is already resolved in jQuery. If you want to use jQuery, but you can not access the html code, and you want to load jQuery You can include jQuery to your site from the CDN url, with javascript too, like this:

<script>  
   (function() {
        var jQ = document.createElement('script'); jQ.type = 'text/javascript'; 
        jQ.async = true;
        jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jQ, s);
   })();
</script>

As the google makes in analitycs code ;)

Community
  • 1
  • 1
Kovge
  • 2,019
  • 1
  • 14
  • 13
0

try this

  document.onkeypress = keyPressFunction;

    function keyPressFunction(e) {
      var s = String.fromCharCode(e.which);
        if ( s === "f"){
         alert ("its f");
        }
        else if ( s === "r"){
         alert ("its r");
        }
    }
PSR
  • 39,804
  • 41
  • 111
  • 151
0

heres a simple function that do exactly what you want -

document.onkeydown=function(e){
    var char= String.fromCharCode(e.keyCode);
    if(char=='f'){
        alert(char);
    }else if(char=='r'){
        alert(char);
    }
}
spaceman12
  • 1,039
  • 11
  • 18