vincent85
July 23rd, 2007, 03:02 AM
I am trying to do this :
Typing a name into the suburb entry field will scroll the suburb select
field to a matching suburb name.
How it work:
1. Pick a state ("ACT" OR "NT")
2. Enter Suburb name into text field
3. See if the Select field scroll to a matching name.
Problem:
If you pick state as "ACT" and enter " WANNIASSA" into select field. The script work.
(DO NOT RELOAD THE PAGE)After that pick state "NT", enter any word into text field (eg, "darwin") . You will see the script doesn't work.
2.(Reload the page) Now pick state as "NT" then typing "Darwin" into text field. Now the script wor
Here is the script:
var index = new Object;
index.isthere = false;
function wordComplete (suburb1,suburb2)
{
var text = document.getElementById(suburb1);
var pick = document.getElementById(suburb2);
if (!index.isthere) listIndex(suburb2);
var suburb = text.value.match(/,*([^,]+)$/);
if (suburb)
{
var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
for (var i = index[name.charAt(0)]; i < pick.options.length; i++)
{
if (pick.options[i].text.toUpperCase().indexOf(name) == 0)
{
pick.selectedIndex = i;
break;
}
else
{
pick.selectedIndex = 0;
}
}
}
}
function listIndex (suburb)
{
var pick=0;
pick = document.getElementById(suburb);
for (var i = pick.options.length; i--;)
{
index[pick.options[i].text.toUpperCase().charAt(0)] = i;
}
index.isthere = true;
}
function subfieldInsert (suburb1,suburb2)
{
var text = document.getElementById(suburb2);
var select = document.getElementById(suburb1);
text.focus();
var suburb = select.options[select.selectedIndex];
if (select.selectedIndex == 0)
{
text.value = '';
return true;
}
var textSuburbs = text.value.split(/\s*,\s*/);
if (!textSuburbs) return false;
var pattern = new RegExp('\s*' + suburb.text + '\s*$');
for (var i = 0; i < textSuburbs.length; i++)
{
if (pattern.exec(textSuburbs[i])) return false;
}
var newvalue = text.value.replace(/(^|,)([^,]*)$/, "$1 " + suburb.text + ', ');
text.value = newvalue;
return false;
}
I hope you understand what i meant and help me iron this bugs.
Thank in advantage
ps. The attachment have all the files. Open it and run and you will understand.
Typing a name into the suburb entry field will scroll the suburb select
field to a matching suburb name.
How it work:
1. Pick a state ("ACT" OR "NT")
2. Enter Suburb name into text field
3. See if the Select field scroll to a matching name.
Problem:
If you pick state as "ACT" and enter " WANNIASSA" into select field. The script work.
(DO NOT RELOAD THE PAGE)After that pick state "NT", enter any word into text field (eg, "darwin") . You will see the script doesn't work.
2.(Reload the page) Now pick state as "NT" then typing "Darwin" into text field. Now the script wor
Here is the script:
var index = new Object;
index.isthere = false;
function wordComplete (suburb1,suburb2)
{
var text = document.getElementById(suburb1);
var pick = document.getElementById(suburb2);
if (!index.isthere) listIndex(suburb2);
var suburb = text.value.match(/,*([^,]+)$/);
if (suburb)
{
var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
for (var i = index[name.charAt(0)]; i < pick.options.length; i++)
{
if (pick.options[i].text.toUpperCase().indexOf(name) == 0)
{
pick.selectedIndex = i;
break;
}
else
{
pick.selectedIndex = 0;
}
}
}
}
function listIndex (suburb)
{
var pick=0;
pick = document.getElementById(suburb);
for (var i = pick.options.length; i--;)
{
index[pick.options[i].text.toUpperCase().charAt(0)] = i;
}
index.isthere = true;
}
function subfieldInsert (suburb1,suburb2)
{
var text = document.getElementById(suburb2);
var select = document.getElementById(suburb1);
text.focus();
var suburb = select.options[select.selectedIndex];
if (select.selectedIndex == 0)
{
text.value = '';
return true;
}
var textSuburbs = text.value.split(/\s*,\s*/);
if (!textSuburbs) return false;
var pattern = new RegExp('\s*' + suburb.text + '\s*$');
for (var i = 0; i < textSuburbs.length; i++)
{
if (pattern.exec(textSuburbs[i])) return false;
}
var newvalue = text.value.replace(/(^|,)([^,]*)$/, "$1 " + suburb.text + ', ');
text.value = newvalue;
return false;
}
I hope you understand what i meant and help me iron this bugs.
Thank in advantage
ps. The attachment have all the files. Open it and run and you will understand.