title: Javascript Counting String with White Space
body: <p>So I need to count the length of a string lively, I mean like twitter, when you type, there is a counter which decreases as more character exist.</p>
  
  <p>The problem is, when you type " " (whitespace), the counter start to count wrong, and be like that until another character exist.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>$(".tweet-content").bind("keydown", function(){
      var tweetlength = $(".tweet-content").html().length;
      $(".char-count").html(126-tweetlength);
  });
  </code></pre>
  
  <p>When I enter <code>asdf</code> for example, it shows <code>122</code> (correct), but when I enter <code>asdf</code> (with a whitespace), it shows <code>116</code> (wrong). But when I enter <code>asdf a</code> (inserting 'a' after the whitespace), it shows <code>120</code> (correct).</p>
  
  <p>So what's wrong with the whitespace?</p>