Click to See Complete Forum and Search --> : Weirdness with text selection


outnabout
02-24-2004, 06:06 PM
Does anyone know if javascript has some issue with text case inside a text selection. I am trying to make a sort of city auto fill field with the following as an example. The issue I notice is that if the cities have a capital letter then it doesn't work as desired. The idea is that you could start typing a name and it would auto fill the rest if it had something that was similar but leave your cursor where it was so you could keep typing. This works great if you do not use any capital letters. Or if you do an alert then things work fine which really puzzled me.

<script language="JavaScript">
var citySize = 4;
var cityArray = new Array(citySize);

cityArray[0] = 'new york';
cityArray[1] = 'portland';
cityArray[2] = 'salt lake city';
cityArray[3] = 'seattle';

function searchCity() {
var searchVal = document.drawerForm.city.value.toLowerCase();
var searchValLen = searchVal.length;
var rangeStart = searchValLen;
alert (rangeStart);

if (searchValLen > 0) {
for (var x=0; x < citySize; x++) {
cityCheckVal = cityArray[x];
cityCharComp = cityCheckVal.substring(0, searchValLen);

if (searchVal == cityCharComp) {
document.drawerForm.city.value = cityCheckVal;
rangeRef = document.drawerForm.city.createTextRange();
rangeRef.moveStart("character", rangeStart);
rangeRef.select();

break;
}
}
}
}


</script>

<form name='drawerForm' method='Post' action=''>
<input type='text' name='city' value='' onkeyup='searchCity();' size='20'>
</form>

dehian
02-26-2004, 04:29 AM
just try to lowercase the cityCharComp value

as below....

<script language="JavaScript">
var citySize = 4;
var cityArray = new Array(citySize);

cityArray[0] = 'New York';
cityArray[1] = 'portland';
cityArray[2] = 'salt lake city';
cityArray[3] = 'seattle';

function searchCity() {
var searchVal = document.drawerForm.city.value.toLowerCase();
var searchValLen = searchVal.length;
var rangeStart = searchValLen;

if (searchValLen > 0) {
for (var x=0; x < citySize; x++) {
cityCheckVal = cityArray[x];
cityCharComp = cityCheckVal.substring(0, searchValLen).toLowerCase();

if (searchVal == cityCharComp) {
document.drawerForm.city.value = cityCheckVal;
rangeRef = document.drawerForm.city.createTextRange();
rangeRef.moveStart("character", rangeStart);
rangeRef.select();

break;
}
}
}
}


</script>

<form name='drawerForm' method='Post' action=''>
<input type='text' name='city' value='' onkeyup='searchCity();' size='20'>
</form>






hope that's what you were lookin' for....

dehian.

outnabout
02-26-2004, 12:36 PM
Thank you for the response. Normally that would be great but its doesn't fix the real problem that if the user types a capital letter in that the script no longer selects the text like it should. I don't know if this is some sort of javascript bug or what. Give it a try. If you type "N" as the first char instead of "n" then it doesn't work.