Question

I am trying to create a mimic autocomplete, as the one jQuery provides is overloaded, and I don't want to use it that much.

var easyBB = {
  spellCheck: function(boolean,options) {
if(boolean === true){
  $('textarea').on('keyup',function() {
    var wordTyped = $(this).val();
  for(var word in options.words){
    var i=0;
     while(wordTyped.indexOf(options.words[word])) {
     $('#responsiveWords').html(options.words[word]);
       i++;
      }
     }
    });

   }
  }
 };

   easyBB.spellCheck(true,{
       words:[
         "ActionScript",
         "AppleScript",
         "Asp",
         "BASIC",
         "C",
         "C++",
         "Clojure",
         "COBOL",
         "ColdFusion",
         "Erlang",
         "Fortran",
         "Groovy",
         "Haskell",
         "Java",
         "JavaScript",
         "Lisp",
         "Perl",
         "PHP",
         "Python",
         "Ruby",
         "Scala",
         "Scheme"
         ]                 
      });

Basically this is going to be for a textarea, the number one issue is how to get the word they are currently typing to work for this.

Next issue is that is is only showing the first word in the words array. Not even resembling the word that it is closest to in character terms. Also it will only show one word, I want it to show any words that are indexOf the value...

Can someone help me and explain to me what I am doing here. I am a self taught person so learning means to actually apply to the code so that is why I am writing a while loop. Just learned .call today :) Sorry irrelevant, anyways any suggestions in what I am doing wrong here?

http://jsbin.com/edolap/1/edit

Was it helpful?

Solution

Try this

var easyBB = {

  spellCheck: function(b,xxx) {
      $('textarea').on('keyup',function() {
        var list = xxx.words;
        var wordTyped = $(this).val();
        $('#responsiveWords').html(""); // reset the list
        if(wordTyped === "") return; // check we have something otherwise you get all results here.
        for (var i = 0; i < list.length; i++) {
          if(list[i].indexOf(wordTyped) === 0){
            $('#responsiveWords').append($("<li>"+list[i]+"</li>")); // create and append items
          }
        }
     });
  }
};


easyBB.spellCheck(true,
                  { words:[
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ]});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top