Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    autocomplete question

    Hi,

    I've recently got a hold of a function that allows for predictive / autocomplete text (shown below). This is fine when Im using it on the first word, however what I'm trying to do is to get it on anything after the first word (ie the second or third word in the text box). If anyone could give some pointers I would be much obliged.

    Cheers

    function autoComplete (dataArray, input, evt) {

    if (temp.length == 0) {
    return;
    }
    var match = false;
    for (var i = 0; i < dataArray.length; i++) {
    if ((match = dataArray[i].toLowerCase().indexOf
    (temp.toLowerCase()) == 0)) {
    break;
    }
    }
    if (match) {
    var typedText = input.value;
    if (typeof input.selectionStart != 'undefined') {
    if (evt.keyCode == 16) {
    return;
    }
    input.value = dataArray[i];
    input.setSelectionRange(typedText.length, input.value.length);
    }
    else if (input.createTextRange) {
    if (evt.keyCode == 16) {
    return;
    }
    input.value = dataArray[i];
    var range = input.createTextRange();
    range.moveStart('character', typedText.length);
    range.moveEnd('character', input.value.length);
    range.select();
    }
    else {
    if (confirm("Are you looking for '" + dataArray[i] + "'?")) {
    input.value = dataArray[i];
    }
    }
    }
    }

  • #2
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    maybe:
    Code:
     if ((match = dataArray[i].toLowerCase().indexOf(temp.toLowerCase()) >= 0)) {
    so it doesn't need to be found at the very beginning
    rm -f /

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks for the reply !

    Unfortunately,

    it still only looks for the first word in the input. Everything else is ignored

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Method to autocomplete the end text is to split the text input at the spaces and examine the charactors after the last space for a match.

    If matched, replace the text with current text less the charactors after the last space plus the matched word.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •