Reputation: 9066
I need to grab the input from keyboard and put it in a div. I also want to grab copy pasted string from the editor. So I am using input event instead of keyup.
To do this I need to grab the char code and convert it to a string. But I found that e.which won't work with input (at least not working with me, not quite sure about others). It is returning 0 everytime.
I want to keep the "input" event. So how do I get the key code for this event?
var editor=document.getElementById('editor');
var content=document.getElementById('content');
editor.addEventListener('input',function(e){
alert(e.which);
});
#editor{
position:relative;
width:500px;
height:400px;
border:1px solid black;
word-wrap: break-word;
padding-left:4px;
padding-right:4px;
padding-bottom:1em;
box-sizing:border-box;
overflow:scroll;
}
<div id='editor' contenteditable='true' ></div>
Upvotes: 3
Views: 4993
Reputation: 388316
It is because input is not a KeyboardEvent and Event.which is a property of KeyboardEvent.
So if you want to get e.which
then you need to use one of the available keyboard events like keypress, keyup or keydown
Upvotes: 2