javascript - detect character before the cursor after keyup in textarea -
i trying implement tagging facebook @friendname. have textarea , wanted detect when user typed in @. how do using keyup listener? possible entered text using keyup? here's have now
$("#recommendtextarea").keyup(function () { var content = $(this).val(); //content box data var go = content.match(start); //content matching @ var name = content.match(word); //content matching @friendname console.log(content[content.length-1]); //if @ available if(go.length > 0) { //if @abc avalable if(name.length > 0) { //do here } } }); most importantly need index of the'@' user entered.
(function ($, undefined) { $.fn.getcursorposition = function() { var el = $(this).get(0); var pos = 0; if('selectionstart' in el) { pos = el.selectionstart; } else if('selection' in document) { el.focus(); var sel = document.selection.createrange(); var sellength = document.selection.createrange().text.length; sel.movestart('character', -el.value.length); pos = sel.text.length - sellength; } return pos; } })(jquery); $("#recommendtextarea").on('keypress', function(e){ var key = string.fromcharcode(e.which); if(key === '*') { var position = $(this).getcursorposition(); alert(position); // position alert($(this).val()); // value } }); i made changes here.
Comments
Post a Comment