Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto complete words in an textarea 1

Status
Not open for further replies.

TanTrazz

Programmer
Aug 18, 2001
54
NL
Hello all,

I've found an script on the internet witch autocompletes words from an array.

This only works for the first line in a textarea.

the question is how can i make it work on multiple lines???

Here is the script


Code:
<script LANGUAGE="JavaScript"  type="text/javascript" type="text/javascript">
<!--
		autoComplete=new Array("Computer opnieuw geïnstalleerd","Spyware verwijderd","Viruscontrole uitgevoerd", "Computer opgehaald","Hardware geïnstalleerd", "Software opnieuw geïnstalleerd","Exchange", "Microsoft", "Microsoft Windows", "Microsoft Word", "Microsoft Excel", "Microsoft Publisher");
								
		function komplett(feld) {
			if (!self.event || String.fromCharCode(event.keyCode).search(/[\w\-0-9\.\,]/)==-1) return;
				sname=feld.value.toLowerCase();
					for (i=0;i<autoComplete.length;i++) {
						if (autoComplete[i].toLowerCase().indexOf(sname)==0 && sname.length!=autoComplete[i].length) {
								feld.value=autoComplete[i];
								(range=feld.createTextRange()).moveStart("character",sname.length);
								range.select();
								break;
						}
					}
		}
								
// -->
</script>


tnx TanTrazz
 
This works pretty well for me if the user doesn't cursor back up and attempt to rework some of the text in the middle of the textarea. However, I suspect there is a way to compensate for that as well.

Code:
function komplett(feld) 
{
 if (!self.event || String.fromCharCode(event.keyCode).search(/[\w\-0-9\.\,]/)==-1 [b]|| feld.value.length == 0[/b]) 
   return;

 sname=feld.value.toLowerCase();
[b] cursorPlace = sname.length + 1;

 lines = new Array();

 while(sname.indexOf('\n') != -1)
 {
  lines[lines.length] = sname.substring(0, sname.indexOf('\n'));
  sname = sname.substring(sname.indexOf('\n')+1);
 }//end while

 lines[lines.length] = sname;

 for(j=0; j<lines.length; j++)
 {
  cursorPlace--;[/b]
  for (i=0;i<autoComplete.length;i++) 
  {
   if (autoComplete[i].toLowerCase().indexOf([b]lines[j][/b])==0 && [b]lines[j][/b].length!=autoComplete[i].length) 
   {
[b]    lines[j] = autoComplete[i];

    feld.value = '';

    for(k=0; k<lines.length; k++)
    {
     feld.value += lines[k];
    }//end for

    range = feld.createTextRange();
    range.moveStart("character",cursorPlace);
    range.moveEnd("character",feld.value.length - cursorPlace);[/b]
    range.select();
    break;
   }
  }
[b] }//end for[/b]
}

With thanks to cLFlaVA and his responses to the post at
Let me know if you have any questions about what I've done. I haven't commented the code.

--Dave
 
Dave,


Thank u very much it worked very fine. You helped me a lot.

TnQ TanTrazz
 
You should give Dave a star if he helped you...

Adam
"Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done." Andy Rooney.
 
Thanks for looking out for me, Adam!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top