AutoComplete <Textarea>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsuns82
    New Member
    • Mar 2007
    • 58

    AutoComplete <Textarea>

    Hi frnds,
    I have got a strange requirement,i.e i have to make a <Textarea>
    with AutoComplete attribute.so that it il suggest the user with a list of
    previously entered values.

    Is thr any way to achieve this using javascript or with AJAX???
    It il be great,if i am given with some samples.



    Thanks in advance,
    sundar
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by gsuns82
    Hi frnds,
    I have got a strange requirement,i.e i have to make a <Textarea>
    with AutoComplete attribute.so that it il suggest the user with a list of
    previously entered values.

    Is thr any way to achieve this using javascript or with AJAX???
    It il be great,if i am given with some samples.



    Thanks in advance,
    sundar
    This isn't something that HTML or CSS can do for you (that I know of), unless there is some new advancement.

    You will probably have to code that in a language such as Perl, PHP, or Python.

    Also, what are you trying to auto complete? Email addresses? Dates?

    Comment

    • gsuns82
      New Member
      • Mar 2007
      • 58

      #3
      Originally posted by numberwhun
      This isn't something that HTML or CSS can do for you (that I know of), unless there is some new advancement.

      You will probably have to code that in a language such as Perl, PHP, or Python.

      Also, what are you trying to auto complete? Email addresses? Dates?
      its a list of descriptions entered by user during previous visit.
      regards,
      sundar

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by gsuns82
        its a list of descriptions entered by user during previous visit.
        regards,
        sundar
        I think you would have to either rely on the system the user is using to offer the suggestions once they start typing (happens today when I fill out forms in my Firefox browser) or you would have to code for it.

        Regards,

        Jeff

        Comment

        • bdappen
          New Member
          • Nov 2008
          • 1

          #5
          You can absolutely do this with Javascript (and/or AJAX). If it's a large number of possible autoComplete entries you'll need to use AJAX for speed reasons, but for shorter lists of autoComplete possibilities you can just use a javascript array. The process consists of four basic components:

          1. Detect current carat position: See http://www.csie.ntu.edu.tw/~b88039/h...lib/caret.html . Basically, onKeyPress or onKeyDown o(and onFocus), ask for the cursor position.

          2. Get the active word: Using the carat position, and the textarea value (str):

          Code:
          var stringToCursor = str.substring(0, caratPosition);
          var start = (stringToCursor.lastIndexOf(" ") + 1); //find where the word starts
          //correct for line breaks (so it works on words seperated by spaces AND line breaks
          	if(start < stringToCursor.lastIndexOf('\n')){start = stringToCursor.lastIndexOf('\n') + 1;};
          var activeWord = str.substring(start, caratPosition);
          3. Find Matches: Compare the active word against a javascript array or a database (ajax) to see if it matches the begining of any autoComplete words.

          4. Autocomplete: Have the aforementioned function automatically replace the active word or create a popup with suggestions which can replace the word using methods similar to step 2 to find the original word fragment and replace it with the new autoCompletion word at the carat (and you'll probably want to reset the carat at the end of the inserted entry (using range.move() or setSelectionRan ge() depending on browser).

          Comment

          Working...