- I have a code which is suppose to update the number of remaining characters left when the user types in a - text input. The code is triggered using- keypressevent. The problem is the code is triggered only after 2- keypress. Why does that happen?
- I have a code to show the key of ASCII code but the character always shows 8 and shows when I press backspace. And how to use - String.fromCharCode(event.keycode} ;method.
- Why is a - eventparameter added to a- function? How does- e.keycodeknow it is displaying the- keycodeof the user's input.
Code Sample
HTML
<div id="page">
  <h1>List King</h1>
  <form id="messageForm">
    <h2>My profile</h2>
    <textarea id="message"></textarea>
    <div id="charactersLeft">180 characters</div>
    <div id="lastKey"></div>
  </form>
</div>
JavaScript
var el;                                                    // Declare variables
function charCount(e) {                                    // Declare function
  var textEntered, charDisplay, counter, lastkey;          // Declare variables
  textEntered = document.getElementById('message').value;  // User's text
  charDisplay = document.getElementById('charactersLeft'); // Counter element
  counter = (180 - (textEntered.length));                  // Num of chars left
  charDisplay.textContent = counter;                       // Show chars left
  lastkey = document.getElementById('lastKey');            // Get last key used
  lastkey.textContent = 'Last key in ASCII code: ' + e.keyCode; // Create msg 
}
el = document.getElementById('message');                   // Get msg element
el.addEventListener('keypress', charCount, false);         // keypress -call charCount()
 
     
     
     
    