How to determine if user is beginning new word in JS?

How would you, with JS or jQuery, determine if what the user is typing is a new word?

What I want to do:

I am writing a documentation tool with autocompletion for different types. If you type f.e. @ it will populate Java Classes in a box, # would populate test classes, etc. Now I don’t want to populate these values, if the user is writing something like an email like yourname@domain.com. So I need the values to populate only when it’s the beginning of the word.

I am aware of keydown, keyup events, etc. I just don’t know how to check for this certain kind of event properly.

One way would be to save every typed letter in a variable and then check if the previous “letter” was a space and if it was, we know it’s a new word. Is this the best/most efficient way to do this?


Source: jquery

Leave a Reply