techfoobar AutoComplete JSfiddle

123 views Asked by At

I would love if techfoobar could help me figure the problem out. If someone can let him know about this question, I would appreciate it.

Either way, I was looking at the fiddle: http://jsfiddle.net/VwNHN/ and I have it working on my site. My only issue is that I can not get it to work with ANY version of Internet Explorer. Could you, or anyone else, help me figure this out?

Thank You!

This is the code: 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 "shell", "test", "snippets", "mootools", "mootools shell", "easy test", "test you snippets", "shell easy test" ];

$('#ta').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));
console.log(y);

// incremental match array
var words = y.split(' '), i;
for(i=words.length-2; i>=0; i--) {
    words[i] = words[i] + ' ' + words[i+1];
}
console.log('Matching against: '+words);

// 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].indexOf(words[i])!==-1) {
            if(matches.indexOf(phrases[j])==-1) {
                matches.push(phrases[j]);
                $('<div>'+phrases[j]+'</div>').appendTo('#matches');
            }
        }
    }
}

//console.log(matches);

}); ​

0

There are 0 answers