-2

Is there really a way with which i could count the number of times, the enter key is pressed in a webpage. Not within an element say, a text box for an example, but rather the whole webpage. say if i open google.com and type something in the search box and hit enter, so the count is one. as such, another search would give 2 enters pressed.any suggestions or ideas?

amflare
  • 4,020
  • 3
  • 25
  • 44
Sri Ram
  • 9
  • 7
  • `java` + `jquery`? Maybe you mean `javascript`? Remember, [`java` is not `javascript`](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java). – u32i64 Mar 06 '17 at 15:29
  • Sure it is possible. But out of interest: Why would you want to count the times enter is pressed? Does the first user to press enter 100times on your site win a price? ;-) – OH GOD SPIDERS Mar 06 '17 at 15:31
  • add a counter for everytime someone searches i'd say. – SC92 Mar 06 '17 at 15:31

2 Answers2

0

You can achieve this by using following approach:

var noOfCounts = 0;

$(document).on('keyup', function(e){
    if (e.which == 13){
        noOfCounts ++;
    }
});
Muhammad Qasim
  • 1,622
  • 14
  • 26
0

You want a JS event listener

MDN Docs

Specifically keydown.

window.addEventListener('keydown', function(e){
    console.log(e.keycode);
});

Then figure out which keycode you need and run your validation and other functions from there.

amflare
  • 4,020
  • 3
  • 25
  • 44