Bertvan Bertvan - 5 months ago 16
jQuery Question

JQuery Facebook like AutoSuggest triggered by "@"

I'm looking for a JQuery (or other framework) plugin, kind of working like the Facebook status message text box.

What it should do:


  • Allow text to be typed freely without triggering the AutoSuggest (as opposed to normal AutoSuggest boxes)

  • Show the AutoSuggest, when triggered by a certain character, such as '@'.



Actually, it should work exactly like the one in FaceBook ... :)

Answer

I would use something like this: http://www.codeproject.com/KB/aspnet/Search_SuggestTextBox.aspx

The good thing about this library is that it is triggered by the text input's onkeyup. This means you can alter their provided JS function to check for the correct key sequence

Maybe something like this:

var shiftDown=false; //Track if SHIFT was the last key pressed.

function searchSuggest(e) 
{
    var key = window.event ? e.keyCode : e.which;

    if (key==40 || key==38)
    {
        shiftDown=false;
        scrolldiv(key); 
    }
    else if (key == 16) // SHIFT WAS PRESSED
    {
        shiftDown=true;
    }
    else if (key == 50 && shiftDown) // 2 WAS PRESSED
    {
        if (searchReq.readyState == 4 || searchReq.readyState == 0) 
        {
            var str = escape(document.getElementById('txtSearch').value);
            strOriginal=str;
            searchReq.open("GET", 'Result.aspx?search=' + str, true);  
            searchReq.onreadystatechange = handleSearchSuggest;
            searchReq.send(null);
            shiftDown=false; 
        }
    }  
    else 
    {   
        shiftDown=false; 
    }  
}