javascript - How to determine if user is beginning new word in JS? -
how you, js or jquery, determine if user typing new word?
what want do:
i writing documentation tool autocompletion different types. if type f.e. @
populate java classes in box, #
populate test classes, etc. don't want populate these values, if user writing email yourname@domain.com
. need values populate when it's beginning of word.
i aware of keydown, keyup events, etc. don't know how check kind of event properly.
one way save every typed letter in variable , check if previous "letter" space , if was, know it's new word. best/most efficient way this?
one way check what's before @
in input box, using selectionstart
:
onload = function() { var io = document.getelementbyid("io"); io.onkeypress = function(e) { if(e.charcode == 64 && ( io.selectionstart == 0 || io.value[io.selectionstart-1].match(/\s/))) document.getelementbyid("ac").innerhtml = "autocomplete!"; else document.getelementbyid("ac").innerhtml = ""; } }
<input id="io"> <div id="ac"></div>
Comments
Post a Comment