How to fire alert once for each new space found in text String?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed">jQuery Event Keypress: Which key was pressed?</a>  </p>
</blockquote>



<p>I'm using below code to fire an alert every time a new space char is encountered as a user types into a text box.
This works until the user enters the first space, as the alert will be fired for each subsequent space.</p>

<p>For this String : 'test ' the alert will be fired once, which is correct</p>

<p>For this String : 'test test' the alert will be fired 5 times, but it should be just fired once.</p>

<p>For this String : 'test test test' the alert should be fired three times.</p>

<p>How can below code be amended so that alert fires just once for each new space character ?</p>

<pre><code>$("#myDiv").keyup(function() {    
    if$("#myDiv").val().indexOf(' ') != -1)
    {
        alert("space found");
    }   ​
};
</code></pre>
