Why does e.keyCode does not catch “{” in my textarea?

262
July 16, 2017, at 5:13 PM

When I press any key in my textarea, it looks like that e.keycode does not function properly. But it always triggers when I press any key in my textarea. Any help please ?

function d(e) { 
  var key = (e.keyCode); 
  if (key == 229) { 
    alert("hello"); 
    return false; 
  } 
}
<textarea id="msg" onkeyup="d(event)">  
</textarea>

I'm not working on pc. Im testing this on android browser.

Answer 1

219 is the keyCode which need to be used

function d(e) { 
  var key = (e.keyCode); 
 
  if (key == 219) { 
    alert("hello"); 
    return false; 
  } 
}
<textarea id="msg" onkeyup="d(event)">  
 </textarea>

Answer 2

The keycode for { is 219. Some browser uses keycode, some uses which. There is also charCode.

function d(e){ 
    var key = e.which || e.keyCode || e.charCode || 0; 
    if (key == 219){ 
         alert("hello"); 
         return false; 
    } 
}
<textarea id="msg" onkeyup="d(event)">  
</textarea>

Answer 3

try this one

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeypress="myFunction(event)">

<script>
function myFunction(event) {
    var x = event.which || event.keyCode;
    if(x == '122'){ alert('hello');}
}
</script>
</body>
</html>
Rent Charter Buses Company
READ ALSO
Native sort function not working correctly when trying to sort array of objects

Native sort function not working correctly when trying to sort array of objects

I am trying to sort array of objects based on an integer propertyWhen using lodash's method _

348
Use an array of objects to construct a new object that will call functions declared in the original array

Use an array of objects to construct a new object that will call functions declared in the original array

I am using a JS router for my new website projectThe router accepts an object: the Key is the URL address and the Value is a function that will get called when the address is reached

323
Simplify toggle and setState updated item&#39;s property

Simplify toggle and setState updated item's property

Toggling item and changing property's value is very common but I still write this kind of code

305
How to load a web page through a button using enter key on keyboard? [on hold]

How to load a web page through a button using enter key on keyboard? [on hold]

There is a button on webpageI want a load web page through a button using enter key

290