0

im trying to implement mention using (@) on a textarea that feeds from MySQL database. this is the code im using below

   var start=/@/ig; // @ Match
                        var word=/@(\w+)/ig; //@abc Match

                        $("#newstatus").live("keyup",function()
                        {
                            var content=$(this).val() //Content Box Data
                            var go= content.match(word); //Content Matching @
                            var name= content.match(word); //Content Matching @abc
                            var dataString = 'searchword='+ name;
//If @ available
                            if(go.length>0)
                            {
                                $("#msgbox").slideDown('show');
                                $("#display").slideUp('show');
                                $("#msgbox").html("Type the name of someone or something...");
//if @abc avalable
                                if(name.length>0)
                                {
                                    $.ajax({
                                        type: "POST",
                                        url: siteurl + "ajax/mention", // Database name search
                                        data: dataString,
                                        cache: false,
                                        success: function(data)
                                        {
                                            $("#msgbox").hide();
                                            $("#display").html(data).show();
                                        }
                                    });
                                }
                            }
                            return false;
                        });

The problem happens when i add more text it keeps showing suggested box, i want to stop searching whenever i start a new sentence after the @WORD im using e.g.

@blackberry is my favorite smartphone ! 

i may use more than one mention in that textarea ! i want your help how do i do that whenever i use @ then i have suggested list whenever i choose i can continue writing and may use another mention

kallo kallo
  • 27
  • 1
  • 7

1 Answers1

0

Solution is:

                    $("#newstatus").live("keyup",function()
                    {
                        var content=$(this).val() //Content Box Data
                        var index= content.lastIndexOf("@"); //Content Matching @
                        var name= content.substr(n+1);; //Content Matching abc, If you want to get @abc then remove +1.
                        var dataString = 'searchword='+ name;

Explanation:

I am searching last position of @ in a given string (using lastIndexOf()) and from that I am getting the string till end using substr().

You no need to use reg exp for this. Regular Exp are very costly, it will consume more resources, According to my knowledge better to ignore regular exp for a small operations.

Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15
  • Hello ! thank you for your reply ! but i dont know why its not working at all , i no longer see the suggestion list ! – kallo kallo Oct 21 '15 at 10:43
  • As i have tested using alert ! the var index always return 0 ! thats why its not working ! can you advise ? – kallo kallo Oct 21 '15 at 10:46
  • Now it works like this : $("#newstatus").live("keyup",function() { var content=$(this).val() //Content Box Data var index= content.lastIndexOf("@"); //Content Matching @ var name= content.substr(index+1);; //Content Matching abc, If you want to get @abc then remove +1. var dataString = 'searchword='+ name; – kallo kallo Oct 21 '15 at 11:05
  • I got a final question , your code works only if there's more than one @ sign, how do i detect @ for the first time ? – kallo kallo Oct 21 '15 at 11:52