PDA

View Full Version : Search a select list


draskc03
06-23-2007, 03:05 AM
I found some code that runs it allows me to seach a select list the only thing i do not like about it is that you have to press "enter" for the effect to stick. So if i have a select list and it is filled with names and I type "chris" if chris is in the list it will show chris in the select box but if i hit "tab" without hitting "enter" first when the select losses focus chris will not be displayed, i must hit enter for it to stick. i tried to change the code to work with the "tab" but it still wont work(it runs just wont stick) any suggestions on how i can get this to work so a user will not have to hit enter they can just hit tab

code is as follows:


function clear_value()
{
obj = window.event.srcElement;
obj.setAttribute("persistValue","");


}

function divert_entry()
{

obj = window.event.srcElement;
//debugger;

if (obj.getAttribute("persistValue") == null)
obj.setAttribute("persistValue","");
var iKey;
var eAny_Event = window.event;

iKey = eAny_Event.keyCode;

var sChr = String.fromCharCode(iKey);

if (iKey == 9) //13 for enter
{

obj.setAttribute("persistValue","");
return true;

}

if (iKey == 8)
{
sDelete_Chr = obj.getAttribute("persistValue");
//obj.setAttribute("persistValue", sDelete_Chr.substring(0,sDelete_Chr.length - 1));
obj.setAttribute("persistValue","");
sChr = '';


}
obj.setAttribute("persistValue",obj.getAttribute("persistValue") + sChr);
lookupItem(obj);


if ((iKey > 33 && iKey < 255) || (iKey == 8) && (iKey !=40) && (iKey != 38))
return false;
//eAny_Event.returnValue = false;


}

function lookupItem(obj)
{

var sCurValue = obj.getAttribute("persistValue").toLowerCase();
var bFound = false;
var iIndex = obj.selectedIndex;
var iNumOptions = obj.options.length;
var iPos = 0;
// Repeat until found or end of list is reached
while ((!bFound) && (iPos < iNumOptions))
{
// Do comparisons in lowercase
bFound = (obj.options[iPos].text.toLowerCase().indexOf(sCurValue)==0);//if 0 the what you typed show up right away in the list
if (bFound)//bfound is already a boolean so its true or false and next line executes if, the if statement is true
iIndex = iPos;
iPos++;
}
if (bFound)
// Updated listbox
obj.selectedIndex = iIndex;

}


<select id="ddlhomepayer" name="ddlhomepayer" onblur="enablePostBack(this.name)" runat="Server" style="width:317px" onkeydown="return divert_entry()" />

The code runs fine but it will not let the value stick

Thanks

rnd me
07-20-2007, 05:11 PM
I found some code that runs it allows me to seach a select list the only thing i do not like about it is that you have to press "enter" for the effect to stick. So if i have a select list and it is filled with names and I type "chris" if chris is in the list it will show chris in the select box but if i hit "tab" without hitting "enter" first when the select losses focus chris will not be displayed, i must hit enter for it to stick. i tried to change the code to work with the "tab" but it still wont work(it runs just wont stick) any suggestions on how i can get this to work so a user will not have to hit enter they can just hit tab




UPDATED code is as follows:


function clear_value()
{
obj = window.event.srcElement;
obj.setAttribute("persistValue","");


}

function divert_entry()
{

obj = window.event.srcElement;
//debugger;

if (obj.getAttribute("persistValue") == null)
obj.setAttribute("persistValue","");
var iKey;
var eAny_Event = window.event;

iKey = eAny_Event.keyCode;

var sChr = String.fromCharCode(iKey);

if (iKey == 9 || iKey==13) //13 for enter
{

obj.setAttribute("persistValue","");
return true;

}

if (iKey == 8)
{
sDelete_Chr = obj.getAttribute("persistValue");
//obj.setAttribute("persistValue", sDelete_Chr.substring(0,sDelete_Chr.length - 1));
obj.setAttribute("persistValue","");
sChr = '';


}
obj.setAttribute("persistValue",obj.getAttribute("persistValue") + sChr);
lookupItem(obj);


if ((iKey > 33 && iKey < 255) || (iKey == 8) && (iKey !=40) && (iKey != 38))
return false;
//eAny_Event.returnValue = false;


}

function lookupItem(obj)
{

var sCurValue = obj.getAttribute("persistValue").toLowerCase();
var bFound = false;
var iIndex = obj.selectedIndex;
var iNumOptions = obj.options.length;
var iPos = 0;
// Repeat until found or end of list is reached
while ((!bFound) && (iPos < iNumOptions))
{
// Do comparisons in lowercase
bFound = (obj.options[iPos].text.toLowerCase().indexOf(sCurValue)==0);//if 0 the what you typed show up right away in the list
if (bFound)//bfound is already a boolean so its true or false and next line executes if, the if statement is true
iIndex = iPos;
iPos++;
}
if (bFound)
// Updated listbox
obj.selectedIndex = iIndex;

}


<select id="ddlhomepayer" name="ddlhomepayer" onblur="enablePostBack(this.name)" runat="Server" style="width:317px" onkeydown="return divert_entry()" />

The code runs fine but it will not let the value stick

Thanks