Skip to content Skip to sidebar Skip to footer

Ajax If More Then One @mention

I am trying to make a facebook and twitter style mention system using jquery ajax php but i have a problem if i try to @mention more then one user. For example if i start to type s

Solution 1:

Looks like you're sending an array which is result of match you in AJAX request.

Though I cannot test it but you can use a lookahead in your regex and use 1st element from resulting array. Negative lookahead (?!.*@\w) is used to make sure we match last element only.

var timer = null;
   var tagword = /@(\w+)(?!.*@\w)/;

   $("body").delegate(".addComment", "keyup", function(e) {
      var value = e.target.value;
      varID = e.target.id;
      clearTimeout(timer);
      timer = setTimeout(function() {
         var contents = value;
         var type = 'smen'; 
         var goname = contents.match(tagword); 

         if (goname != undefined) {
            var data = 'f=' +type+ '&menFriend=' +goname[1] +'&posti='+ID;
            $.ajax({
               type: "POST",
               url: requestUrl + "searchuser",
               data: data,
               cache: false,
               beforeSend: function() {
                  // Do Something
               },
               success: function(response) {
                  if(response){
                     $(".menlist"+ID).show().html(response);
                  } else {
                     $(".menlist"+ID).hide().empty();
                  }
               }
            });
         }
      }, 500);
   });

Post a Comment for "Ajax If More Then One @mention"