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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

autocomplete

Status
Not open for further replies.

mwpc

Programmer
Joined
Apr 28, 2002
Messages
53
Location
US
The following script will autocomplete a text input field with ONCHANGE of the select field

I would like to modify it so that the select fields add to the original value rather than replacing it, i.e.

word1 phrase one

Thanks,
Mike


<SCRIPT>
function matchFieldSelect (field, select, value) {
var property = value ? 'value' : 'text';
var found = false;
for (var i = 0; i < select.options.length; i++)
if ((found = select.options[property].indexOf(field.value) ==
0))
break;
if (found)
select.selectedIndex = i;
else
select.selectedIndex = -1;
if (field.createTextRange) {
var cursorKeys =&quot;8;46;37;38;39;40;33;34;35;36;45;&quot;
if (cursorKeys.indexOf(event.keyCode+&quot;;&quot;) == -1) {
var r1 = field.createTextRange()
var oldValue = r1.text;
var newValue = found ? select.options[property] : oldValue;
if (newValue != field.value) {
field.value = newValue
var rNew = field.createTextRange()
rNew.moveStart('character', oldValue.length)
rNew.select()
}
}
}
}

</SCRIPT>

Select term to fill in text field
<FORM NAME=&quot;aForm&quot;>
<SELECT NAME=&quot;words&quot; SIZE=&quot;1&quot; ONCHANGE=&quot;this.form.word.value = this.options[this.selectedIndex].text&quot;>
<OPTION>SELECT WORD
<OPTION>word1
<OPTION>word2
<OPTION>word3
<OPTION>word4
</SELECT>
  
<SELECT NAME=&quot;words2&quot; SIZE=&quot;1&quot; ONCHANGE=&quot;this.form.word.value = this.options[this.selectedIndex].text&quot;>
<OPTION>SELECT PHRASE
<OPTION>phrase one
<OPTION>phrase two
<OPTION>phrase three
<OPTION>phrase four
</SELECT><BR><BR>
<INPUT TYPE=&quot;text&quot; NAME=&quot;word&quot; ONKEYUP=&quot;matchFieldSelect(this, this.form.words)&quot; SIZE=25>

</FORM>
 
<SCRIPT>
function matchFieldSelect (field, select, value) {
var property = value ? 'value' : 'text';
var found = false;
for (var i = 0; i < select.options.length; i++)
if ((found = select.options[property].indexOf(field.value) ==
0))
break;
if (found)
select.selectedIndex = i;
else
select.selectedIndex = -1;
if (field.createTextRange) {
var cursorKeys =&quot;8;46;37;38;39;40;33;34;35;36;45;&quot;
if (cursorKeys.indexOf(event.keyCode+&quot;;&quot;) == -1) {
var r1 = field.createTextRange()
var oldValue = r1.text;
var newValue = found ? select.options[property] : oldValue;
if (newValue != field.value) {
field.value = oldValue + newValue
var rNew = field.createTextRange()
rNew.moveStart('character', oldValue.length)
rNew.select()
}
}
}
}

</SCRIPT>
&quot;did you just say Minkey?, yes that's what I said.&quot;

MrGreed
 
Thanks, MrGreed

However...

I am missing something here. Apparently the form portion of the page is not dependent on the top script, but rather operates on its own.


<SELECT NAME=&quot;words2&quot; SIZE=&quot;1&quot; ONCHANGE=&quot;this.form.word.value = this.options[this.selectedIndex].text&quot;>
<OPTION>SELECT PHRASE
<OPTION>phrase one
<OPTION>phrase two
<OPTION>phrase three
<OPTION>phrase four
</SELECT><BR><BR>
<INPUT TYPE=&quot;text&quot; NAME=&quot;word&quot; ONKEYUP=&quot;matchFieldSelect(this, this.form.words)&quot; SIZE=25>


Thanks again
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top