Auto Suggest Keywords In Textarea

Text area auto suggest keyword using bootstrap

function doGetCaretPosition(ctrl) {
    var CaretPos = 0;
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    } else if (ctrl.selectionStart || ctrl.selectionStart == '0') CaretPos = ctrl.selectionStart;
    return (CaretPos);
}


var maxMatch = 20; // the longest match interested in

var phrases = [ // your words/phrases array
"ActionScript", "AppleScript",
    "Asp", "BASIC", "C", "C++",
    "Clojure", "COBOL", "ColdFusion", "Erlang",
    "Fortran", "Groovy", "Haskell", "Java",
    "JavaScript", "Lisp", "Perl", "PHP",
    "Python", "Ruby", "Scala", "Scheme", "jquery", "asp.net", "mvc", "csharp", ". NET", "MySQL", "Oracle", "PostgreSQL"];

$('#taxbox').keyup(function () {
    // clear current match list
    $('#matches').empty();
    // get cursor position
    var x = doGetCaretPosition(this);
    // get a string from x-maxMatch to x
    var startPos = x - maxMatch < 0 ? 0 : x - maxMatch;
    var y = $.trim(this.value.substring(startPos, x));
    // incremental match array
    var words = y.split(' '),
        i;
    for (i = words.length - 2; i >= 0; i--) {
        words[i] = words[i] + ' ' + words[i + 1];
    }
    // the match list (use this as your auto-complete list)
    var matches = [],
        j;
    // check in reverse order to have most relevant matches up front
    for (i = 0; i < words.length; i++) {
        for (j = 0; j < phrases.length; j++) {
            if (words[i].length >= 2 && phrases[j].toLowerCase().indexOf(words[i]) !== -1) {
                if (matches.indexOf(phrases[j].toLowerCase()) == -1) {
                    matches.push(phrases[j]);
                    $('<a href=""><span class="label label-success">' + phrases[j] + '</span></a><span>&nbsp;</span>').appendTo('#matches');
                }
            }
        }
    }
});

$(".label").live({
    mouseenter: function () {
        $(this).removeClass('label-success').addClass('label-warning');
    },
    mouseleave: function () {
        $(this).removeClass('label-warning').addClass('label-success');
    }
});
Auto Suggest Keywords In Textarea Auto Suggest Keywords In Textarea Reviewed by Bhaumik Patel on 11:28 AM Rating: 5