0
  1. 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 keypress event. The problem is the code is triggered only after 2 keypress. Why does that happen?

  2. 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.

  3. Why is a event parameter added to a function ? How does e.keycode know it is displaying the keycode of 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()
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CoDINGinDARK
  • 244
  • 4
  • 16

2 Answers2

1

1) Use 'keyup' insted of 'keypress'.

2) lastkey.textContent = 'Last key in ASCII code: ' + String.fromCharCode(e.keyCode);

3) Don't know much about it. event parameter is added to catch the fired event. It has all the property of the fired event like for 'keyup' it has 'charcode', keycode', 'key' etc.

Siddhant
  • 11
  • 3
1
  1. The keypress event is triggered before the input's value is updated, this is why the counter is not up to date. I'd suggest to listen to the input event if you don't need the keyCode. You can also listen to keyup (but the update won't be straight forward) or the combination of keypress and keyup (from "How to get text of an input text box during onKeyPress?", I updated the right answer's fiddle).
    • Actually, the keypress event doesn't seem to be triggered when you press backspace. That been said, an other solution to ignore "muted" keys ("backspace", "shift", "command" and so on) is to listen to the "input" event (but you won't have access to event.keyCode). Otherwise you can also ignore your code when the keyCode is not relevant.
    • In regard to String.fromCharCode, simply use String.fromCharCode(event.keyCode) to get the keyCode's "human" equivalent.
  2. The event passed to the function is an object containing all the informations about this given event, including the pressed key.
Community
  • 1
  • 1
Gabin
  • 920
  • 10
  • 26
  • 1st question is not by a problem,it works with `keyup` do you have a particular reason.Dont say it comes from somewhere else,show it in your code.@gabin – CoDINGinDARK Apr 11 '15 at 13:16
  • 1
    I copied and pasted your code in a [jsfiddle](http://jsfiddle.net/yj1pj18o/). Open your console and type in the input. As you'll see, `keypress` is triggered every time you press a key. – Gabin Apr 11 '15 at 13:39
  • I type a character it doesn't fire and next character I type it fires.In the sense I type a 1 character nothing happens next I type another character (so I have typed 2 characters)after I type in my 2nd char the event fires and shows ( _180-1character_) 179 characters instead of 178 chars left. – CoDINGinDARK Apr 12 '15 at 06:59
  • 1
    Actually the keypress event triggers before the input's value is updated. I'd suggest to listen to the "input" event if you don't need the keyCode. Otherwise you can listen to "keyup" but the update won't be "straight forward". There's also an other solution from "[How to get text of an input text box during onKeyPress?](http://stackoverflow.com/questions/11365686/how-to-get-text-of-an-input-text-box-during-onkeypress)" (I've updated [the fiddle](http://jsfiddle.net/VDd6C/975/) for your needs). – Gabin Apr 12 '15 at 08:08
  • https://jsfiddle.net/mpp522L5/ssss ASCII code doesn't update to letters please say whats wromg – CoDINGinDARK Apr 12 '15 at 09:25
  • Your fiddle seems to work as expected. I see "a" when I press "a" and "b" when I press "b". Note that some keys don't have readable equivalents (shift, command, ...). What's your browser? And version? – Gabin Apr 12 '15 at 10:02
  • Latest version of mozilla firefox. – CoDINGinDARK Apr 12 '15 at 12:42