Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to bind enter click to onkeyup event of a field

So in normal situation where jquery i allowed and i can bind the field onkeyup i would use:

$('#something').keyup(function(e) {
    var enterKey = 13;
    if (e.which == enterKey){
        somefunction();
     }
 });

however i cannot use this and will have to use something like:

<input id="something" onkeyup="onkeyup_colfield_check(this)" type="text">
 function onkeyup_colfield_check(e){
    var enterKey = 13;
        if (e.which == enterKey){
            somefunction();
        }
}

However this doesn't work like the above.

how can i achieve the same result like the first example but with something like that?

like image 522
Neta Meta Avatar asked Dec 30 '12 02:12

Neta Meta


1 Answers

You need to pass in event as an argument, not this.

<input id="something" onkeyup="onkeyup_colfield_check(event)" type="text">

Also, to be fully compatible with all major browsers, you may want to use the following for detecting te key code of the key pressed.

var charCode = (typeof e.which === "number") ? e.which : e.keyCode;
like image 116
jeremy Avatar answered Oct 12 '22 02:10

jeremy