Possible Duplicate:
jQuery Event Keypress: Which key was pressed?
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.
For this String : 'test ' the alert will be fired once, which is correct
For this String : 'test test' the alert will be fired 5 times, but it should be just fired once.
For this String : 'test test test' the alert should be fired three times.
How can below code be amended so that alert fires just once for each new space character ?
$("#myDiv").keyup(function() {
if$("#myDiv").val().indexOf(' ') != -1)
{
alert("space found");
}
};