02 May 2008

JavaScript Smart Dropdown, Smart ComboBox, JavaScript AutoComplete

Just use the storeActCode array to story your autocomplete values and Bingo !!!
You have a JavaScript Auto Complete.
This array can also be generated dynamically by PHP or JSP or any other scripting language using values
Populated from the DataBase.

Simply do not declare this array but make it in PHP.
And then give the source of this script as …

<script language="javascript" src="array.php"> </script>

This line gives an instance to the Array Declaration in the PHP file and Whack … no need for the

Lengthy Google Type AutoComplete.

<html>
<head>
<script>
var smartTimer = 0;
function suggestData(activityTextVal) {
var storeActCode = new Array('A','ABC','ABCD','DEF','DFGR','FDFD','ASD');
var resultActCodeList = new Array();
//storeActCode = {'a','abc','abcd','def','dfgr','fdfdf','asd'};
if(activityTextVal.length > 0)
    {
    for(var i=0; i<storeActCode.length; i++) {
    if(storeActCode[i].substring(0,activityTextVal.length) == activityTextVal.toUpperCase()){
    resultActCodeList.push(storeActCode[i]);
    }
}

populateListBox(resultActCodeList);
}
}

function populateListBox(resultActCodeList)
{
document.getElementById("ActivityCodes").length=0;
if(resultActCodeList.length > 0){
for(var i=0; i<resultActCodeList.length; i++ ){
document.getElementById("ActivityCodes").options[i] =new Option(resultActCodeList[i],resultActCodeList[i],"");
}
}
}
function  doHide()
{
document.getElementById('ActivityDiv').style.display='none';
document.getElementById('ActivityCodes').style.display='none';
}
</script>
</head>
<body>
<form name="form">
<input type="text" tabindex="1" maxlength="4" name="ActivityText" onblur="smartTimer=setTimeout('doHide()', 200);" id="ActivityText" style="width:70px" class="ApInptTxt"
onKeyDown="javascript:if(event.keyCode==40 && document.getElementById('ActivityDiv').style.display!='none'){document.getElementById('ActivityDiv').style.display='block';document.getElementById('ActivityCodes').style.display='block';document.form.ActivityCodes.focus();}"
onKeyUp="javascript:if(this.value.toString().length >0){
document.getElementById('ActivityDiv').style.display='block';
document.getElementById('ActivityCodes').style.display='block';
suggestData(document.getElementById('ActivityText').value);
}else{
document.getElementById('ActivityDiv').style.display='none';
document.getElementById('ActivityCodes').style.display='none';
}
clearTimeout(smartTimer);"
value="">
&nbsp;&nbsp;
<div id="ActivityDiv" >
<select  onfocus="clearTimeout(smartTimer);" onblur="smartTimer=setTimeout('doHide()', 200);"
onclick="document.getElementById('ActivityDiv').style.display='none';
document.getElementById('ActivityCodes').style.display='none';document.getElementById('ActivityText').focus();"
onkeydown=" if(event.keyCode==13){document.getElementById('ActivityText').value=this.value;document.getElementById('ActivityDiv').style.display='none';
document.getElementById('ActivityCodes').style.display='none';document.getElementById('ActivityText').focus();}"  class="ApInptSelect" size="5" style="width:70px;display:none" name="ActivityCodes" id="ActivityCodes" onchange="document.getElementById('ActivityText').value=this.value;" onblur="if(document.getElementById('ActivityDiv').style.display!='none' ){document.getElementById('ActivityDiv').style.display='none'};" >
</select>
</div>
</form>
</body></html>